slack-smart-bot 1.15.0 → 1.15.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +34 -1
  3. data/img/chat_gpt_attach_image.png +0 -0
  4. data/lib/slack/smart-bot/ai/open_ai/models.rb +19 -10
  5. data/lib/slack/smart-bot/ai/open_ai/send_gpt_chat.rb +14 -10
  6. data/lib/slack/smart-bot/comm/dont_understand.rb +23 -6
  7. data/lib/slack/smart-bot/comm/get_user_info.rb +9 -10
  8. data/lib/slack/smart-bot/comm/respond.rb +56 -28
  9. data/lib/slack/smart-bot/comm/send_file.rb +17 -6
  10. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat.rb +885 -129
  11. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_add_collaborator.rb +3 -3
  12. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_copy_session.rb +132 -15
  13. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_delete_session.rb +1 -1
  14. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_get_prompts.rb +50 -12
  15. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_list_sessions.rb +99 -34
  16. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_share_session.rb +12 -2
  17. data/lib/slack/smart-bot/commands/general/ai/open_ai/open_ai_chat_use_model.rb +36 -25
  18. data/lib/slack/smart-bot/commands/general/bot_help.rb +29 -24
  19. data/lib/slack/smart-bot/commands/general/poster.rb +0 -1
  20. data/lib/slack/smart-bot/commands/general/see_announcements.rb +1 -1
  21. data/lib/slack/smart-bot/commands/general/summarize.rb +22 -8
  22. data/lib/slack/smart-bot/commands/general_bot_commands.rb +156 -55
  23. data/lib/slack/smart-bot/commands/on_bot/general/bot_stats.rb +13 -11
  24. data/lib/slack/smart-bot/commands/on_extended/bot_rules.rb +21 -17
  25. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +2 -2
  26. data/lib/slack/smart-bot/commands.rb +19 -19
  27. data/lib/slack/smart-bot/process_first.rb +38 -35
  28. data/lib/slack/smart-bot/treat_message.rb +57 -56
  29. data/lib/slack/smart-bot/utils/download_http_content.rb +91 -0
  30. data/lib/slack/smart-bot/utils/get_authorizations.rb +41 -0
  31. data/lib/slack/smart-bot/utils/get_keywords.rb +33 -0
  32. data/lib/slack/smart-bot/utils/get_openai_sessions.rb +46 -6
  33. data/lib/slack/smart-bot/utils/get_teams.rb +9 -1
  34. data/lib/slack/smart-bot/utils/save_stats.rb +13 -5
  35. data/lib/slack/smart-bot/utils/transform_to_slack_markdown.rb +36 -0
  36. data/lib/slack/smart-bot/utils/update_openai_sessions.rb +9 -4
  37. data/lib/slack/smart-bot/utils.rb +48 -44
  38. data/lib/slack-smart-bot.rb +10 -10
  39. data/whats_new.txt +27 -1
  40. metadata +49 -8
@@ -17,10 +17,10 @@ class SlackSmartBot
17
17
  elsif @active_chat_gpt_sessions[team_id_user].key?(Thread.current[:dest])
18
18
  session_name = @active_chat_gpt_sessions[team_id_user][Thread.current[:dest]]
19
19
  else
20
- session_name = ''
20
+ session_name = ""
21
21
  end
22
22
 
23
- if session_name != "" and @active_chat_gpt_sessions[team_id_user].key?(Thread.current[:thread_ts])
23
+ if @active_chat_gpt_sessions[team_id_user].key?(Thread.current[:thread_ts])
24
24
  collaborator = find_user(cname)
25
25
  team_id_user_collaborator = collaborator.team_id + "_" + collaborator.name
26
26
  unless @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:collaborators].include?(team_id_user_collaborator)
@@ -32,7 +32,7 @@ class SlackSmartBot
32
32
  @chat_gpt_collaborating[team_id_user_collaborator][Thread.current[:thread_ts]] ||= { team_creator: team_id, user_creator: user.name, session_name: session_name }
33
33
  respond "Now <@#{collaborator.name}> is a collaborator of this session only when on a thread.\nIn case you don't want to send a message as a prompt, just start the message with hyphen (-)."
34
34
  else
35
- respond "You can add collaborators for the chatGPT session only when started on a thread and using a session name."
35
+ respond "You can add collaborators for the chatGPT session only when started on a thread."
36
36
  end
37
37
  end
38
38
  end
@@ -3,7 +3,7 @@ class SlackSmartBot
3
3
  module General
4
4
  module AI
5
5
  module OpenAI
6
- def open_ai_chat_copy_session(team_orig, user_orig, session_name, new_session_name)
6
+ def open_ai_chat_copy_session(team_orig, user_orig, session_name, new_session_name, temporary_session: false)
7
7
  if user_orig == ""
8
8
  save_stats(__method__)
9
9
  else
@@ -13,28 +13,71 @@ class SlackSmartBot
13
13
  user = Thread.current[:user].dup
14
14
  team_id = user.team_id
15
15
  if user_orig == ""
16
- user_orig = user.name
16
+ user_orig = user.name
17
17
  team_orig = team_id
18
18
  end
19
19
  team_orig = team_id if team_orig == ""
20
+
20
21
  dest = Thread.current[:dest]
21
22
  get_openai_sessions()
22
23
  orig_team_id_user = team_orig + "_" + user_orig
23
24
  team_id_user = team_id + "_" + user.name
24
25
 
26
+ if temporary_session
27
+ if @open_ai.key?(team_id_user) and @open_ai[team_id_user][:chat_gpt][:sessions].key?("") and
28
+ @open_ai[team_id_user][:chat_gpt][:sessions][""].key?(:thread_ts)
29
+ @open_ai[team_id_user][:chat_gpt][:sessions][""][:thread_ts].each do |thread_ts|
30
+ if thread_ts != Thread.current[:thread_ts] && @listening[:threads].key?(thread_ts)
31
+ unreact :running, thread_ts, channel: @listening[:threads][thread_ts]
32
+ message_chatgpt = ":information_source: I'm sorry, but I'm no longer listening to this thread since you started a new temporary session."
33
+ respond message_chatgpt, @listening[:threads][thread_ts], thread_ts: thread_ts
34
+ @listening[team_id_user].delete(thread_ts)
35
+ @listening[:threads].delete(thread_ts)
36
+ @open_ai[team_id_user][:chat_gpt][:sessions][""][:collaborators].each do |team_id_user_collaborator|
37
+ if @listening.key?(team_id_user_collaborator)
38
+ @listening[team_id_user_collaborator].delete(thread_ts)
39
+ end
40
+ if @chat_gpt_collaborating.key?(team_id_user_collaborator) && @chat_gpt_collaborating[team_id_user_collaborator].key?(thread_ts)
41
+ @chat_gpt_collaborating[team_id_user_collaborator].delete(thread_ts)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ if user_orig != user.name and dest[0] == "D" and @open_ai.key?(orig_team_id_user) and
50
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions].key?(session_name) and
51
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:shared].size > 0
52
+ members_shared = []
53
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:shared].each do |shared|
54
+ members_shared += get_channel_members(shared)
55
+ end
56
+ members_shared.uniq!
57
+ else
58
+ members_shared = []
59
+ end
25
60
  if !@open_ai.key?(orig_team_id_user)
26
61
  respond "*ChatGPT*: The user *#{user_orig}* doesn't exist."
62
+ return false
27
63
  elsif !@open_ai[orig_team_id_user][:chat_gpt][:sessions].key?(session_name)
28
64
  respond "*ChatGPT*: The session *#{session_name}* doesn't exist."
29
- elsif user_orig != user.name and
30
- !@open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:public] and
65
+ return false
66
+ elsif user_orig != user.name and dest[0] == "D" and
67
+ !@open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:public] and
68
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:shared].size > 0 and
69
+ !members_shared.include?(user.id)
70
+ respond "*ChatGPT*: The session *#{session_name}* doesn't exist or it is not shared."
71
+ elsif user_orig != user.name and dest[0] != "D" and
72
+ !@open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:public] and
31
73
  !@open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:shared].include?(dest)
32
74
  respond "*ChatGPT*: The session *#{session_name}* doesn't exist or it is not shared."
75
+ return false
33
76
  else
34
77
  @open_ai[team_id_user] ||= {}
35
78
  @open_ai[team_id_user][:chat_gpt] ||= {}
36
- @open_ai[team_id_user][:chat_gpt][:sessions] ||= {}
37
- session_orig = @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name]
79
+ @open_ai[team_id_user][:chat_gpt][:sessions] ||= {}
80
+ session_orig = @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name].deep_copy
38
81
  open_ai_new_session = {
39
82
  team_creator: team_id,
40
83
  user_creator: user.name,
@@ -48,13 +91,40 @@ class SlackSmartBot
48
91
  copy_of_team: team_orig,
49
92
  copy_of_user: user_orig,
50
93
  users_copying: [],
94
+ temp_copies: 0,
95
+ own_temp_copies: 0,
51
96
  public: false,
52
97
  description: session_orig[:description],
53
- tag: session_orig[:tag]
98
+ tag: session_orig[:tag],
99
+ live_content: session_orig[:live_content],
100
+ static_content: session_orig[:static_content],
101
+ authorizations: session_orig[:authorizations],
54
102
  }
55
- new_session_name = session_name if new_session_name == ""
103
+ if !temporary_session and (team_orig != team_id or user_orig != user.name)
104
+ open_ai_new_session[:authorizations] = {}
105
+ message_auth = "\n:lock: Authorizations were removed on copy."
106
+ elsif !temporary_session and session_orig[:copy_of_session] != "" and
107
+ (session_orig[:copy_of_team] != team_id or session_orig[:copy_of_user] != user.name)
108
+ open_ai_new_session[:authorizations] = {}
109
+ message_auth = "\n:lock: Authorizations were removed on copy."
110
+ else
111
+ message_auth = ""
112
+ end
113
+ message_history = ""
114
+ if team_orig != team_id or user_orig != user.name
115
+ if session_orig.key?(:static_content)
116
+ session_orig[:static_content].each do |cont|
117
+ if cont.match?(/\AHistory of <#(\w+)>\Z/im) #jal66
118
+ respond "*ChatGPT*: The session *#{session_name}* contains the history of a Slack Channel. Sorry, but I can't copy it."
119
+ return false
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ new_session_name = session_name if new_session_name == "" and !temporary_session
56
126
  session_names = @open_ai[team_id_user][:chat_gpt][:sessions].keys
57
- if session_names.include?(new_session_name)
127
+ if session_names.include?(new_session_name) and !temporary_session
58
128
  number = session_names.join("\n").scan(/^#{new_session_name}(\d+)$/).max
59
129
  if number.nil?
60
130
  number = "1"
@@ -63,23 +133,70 @@ class SlackSmartBot
63
133
  end
64
134
  new_session_name = "#{new_session_name}#{number}"
65
135
  end
136
+
66
137
  @open_ai[team_id_user][:chat_gpt][:sessions][new_session_name] = open_ai_new_session
138
+ @ai_gpt[team_id_user][""] = [] if temporary_session and @ai_gpt.key?(team_id_user)
67
139
 
68
140
  if user_orig != user.name or team_id != team_orig
69
- @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:users_copying] ||= []
70
- @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:users_copying] << user.name
141
+ if temporary_session
142
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:temp_copies] ||= 0
143
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:temp_copies] += 1
144
+ else
145
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:users_copying] ||= []
146
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:users_copying] << user.name
147
+ end
71
148
  update_openai_sessions("", team_id: team_orig, user_name: user_orig)
149
+ else
150
+ if temporary_session
151
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:own_temp_copies] ||= 0
152
+ @open_ai[orig_team_id_user][:chat_gpt][:sessions][session_name][:own_temp_copies] += 1
153
+ update_openai_sessions("", team_id: team_orig, user_name: user_orig)
154
+ end
72
155
  end
73
156
 
74
157
  get_openai_sessions(session_name, team_id: team_orig, user_name: user_orig)
75
158
  @ai_gpt[team_id_user] ||= {}
76
- @ai_gpt[team_id_user][new_session_name] = @ai_gpt[orig_team_id_user][session_name].dup
77
- update_openai_sessions(new_session_name, team_id: team_id, user_name: user.name)
159
+ if @ai_gpt.key?(orig_team_id_user) and @ai_gpt[orig_team_id_user].key?(session_name)
160
+ @ai_gpt[team_id_user][new_session_name] = @ai_gpt[orig_team_id_user][session_name].deep_copy
161
+ else
162
+ @ai_gpt[team_id_user][new_session_name] = []
163
+ end
164
+ update_openai_sessions(new_session_name, team_id: team_id, user_name: user.name) unless temporary_session
165
+
78
166
  if user_orig != user.name or team_id != team_orig
79
- respond "*ChatGPT*: Session #{session_name} (#{user_orig}) copied to #{new_session_name}.\nNow you can call `^chatGPT #{new_session_name}` to use it."
167
+ if temporary_session
168
+ res_message = "*ChatGPT*: Session *#{session_name}* (#{user_orig}) copied to be used as a temporary session.#{message_auth}#{message_history}"
169
+ else
170
+ res_message = "*ChatGPT*: Session *#{session_name}* (#{user_orig}) copied to #{new_session_name}.#{message_auth}#{message_history}\nNow you can call `^chatGPT #{new_session_name}` to use it."
171
+ end
80
172
  else
81
- respond "*ChatGPT*: Session #{session_name} copied to #{new_session_name}.\nNow you can call `^chatGPT #{new_session_name}` to use it."
173
+ if temporary_session
174
+ res_message = "*ChatGPT*: Session *#{session_name}* copied to be used as a temporary session.#{message_auth}#{message_history}"
175
+ else
176
+ res_message = "*ChatGPT*: Session *#{session_name}* copied to #{new_session_name}.#{message_auth}#{message_history}\nNow you can call `^chatGPT #{new_session_name}` to use it."
177
+ end
178
+ end
179
+ if !@open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:live_content].nil? and
180
+ @open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:live_content].size > 0
181
+ live_content = "\n:globe_with_meridians: *Live content*:\n\t\t - `#{@open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:live_content].join("`\n\t\t - `")}`"
182
+ res_message += live_content
183
+ end
184
+ if !@open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:static_content].nil? and
185
+ @open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:static_content].size > 0
186
+ static_content = "\n:pushpin: *Static content*:\n\t\t - `#{@open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:static_content].join("`\n\t\t - `")}`"
187
+ res_message += static_content
188
+ end
189
+
190
+ if !@open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:authorizations].nil? and
191
+ @open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:authorizations].size > 0
192
+ auth = "\n:lock: *Authorizations*:\n"
193
+ @open_ai[team_id_user][:chat_gpt][:sessions][new_session_name][:authorizations].each do |host, header|
194
+ auth += "\t\t - `#{host}`: `#{header.keys.join("`, `")}`\n"
195
+ end
196
+ res_message += auth
82
197
  end
198
+ respond res_message
199
+ return true
83
200
  end
84
201
  end
85
202
  end
@@ -31,7 +31,7 @@ class SlackSmartBot
31
31
  end
32
32
 
33
33
  @open_ai[team_id_user][:chat_gpt][:sessions].delete(session_name)
34
-
34
+ @ai_gpt[team_id_user].delete(session_name) if @ai_gpt.key?(team_id_user)
35
35
  update_openai_sessions(session_name)
36
36
  respond "*ChatGPT*: Session *#{session_name}* deleted."
37
37
  else
@@ -6,29 +6,67 @@ class SlackSmartBot
6
6
  def open_ai_chat_get_prompts(session_name)
7
7
  save_stats(__method__)
8
8
 
9
- user = Thread.current[:user].dup
10
9
  team_id_user = Thread.current[:team_id_user]
11
-
10
+
12
11
  @active_chat_gpt_sessions[team_id_user] ||= {}
13
12
 
14
13
  get_openai_sessions(session_name)
15
-
14
+
16
15
  if @open_ai[team_id_user][:chat_gpt][:sessions].key?(session_name)
17
- prompts = @ai_gpt[team_id_user][session_name].join("\n")
18
- prompts.gsub!(/^Me>\s*/, "\nMe> ")
19
- prompts.gsub!(/^chatGPT>\s*/, "\nchatGPT> ")
20
- if prompts.length > 3000
16
+ prompts = ""
17
+ prompts_array = @ai_gpt[team_id_user][session_name]
18
+ prompts_array.each do |prompt|
19
+ if prompt[:role] == "user" and prompt[:content].size > 0 and prompt[:content][0][:type] == "text"
20
+ if prompt[:content][0].key?(:clean_text)
21
+ #if not a static content
22
+ if @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:static_content].nil? or
23
+ !@open_ai[team_id_user][:chat_gpt][:sessions][session_name][:static_content].include?(prompt[:content][0][:clean_text])
24
+ prompts += "\n\n:runner: *User>* #{prompt[:content][0][:clean_text]}\n"
25
+ end
26
+ else
27
+ prompts += "\n\n:runner: *User>* #{prompt[:content][0][:text]}\n"
28
+ end
29
+ elsif prompt[:role] == "user" and prompt[:content].size > 0 and prompt[:content][0][:type] != "text"
30
+ prompts += "\n\n:runner: *User>* Attached file type #{prompt[:content][0][:type]}\n"
31
+ elsif prompt[:role] == "assistant" and prompt[:content].size > 0 and prompt[:content][0][:type] == "text"
32
+ prompts += "\n:speech_balloon: *chatGPT>* #{prompt[:content][0][:text]}\n"
33
+ elsif prompt[:role] == "system" and prompt[:content].size > 0 and prompt[:content][0][:type] == "text"
34
+ prompts += "\n:robot_face: *Context:* #{prompt[:content][0][:text]}\n"
35
+ else
36
+ prompts += "Attention! This message is not in the expected format.\n"
37
+ end
38
+ end
39
+ if !@open_ai[team_id_user][:chat_gpt][:sessions][session_name][:live_content].nil? and
40
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:live_content].size > 0
41
+ prompts += "\n:globe_with_meridians: *Live content:*\n"
42
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:live_content].each do |live_content|
43
+ prompts += "\t\t - #{live_content}\n"
44
+ end
45
+ end
46
+ if !@open_ai[team_id_user][:chat_gpt][:sessions][session_name][:static_content].nil? and
47
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:static_content].size > 0
48
+ prompts += "\n:pushpin: *Static content:*\n"
49
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:static_content].each do |static_content|
50
+ prompts += "\t\t - #{static_content}\n"
51
+ end
52
+ end
53
+ #authorizations
54
+ if !@open_ai[team_id_user][:chat_gpt][:sessions][session_name][:authorizations].nil? and
55
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:authorizations].size > 0
56
+ prompts += "\n:lock: *Authorizations:*\n"
57
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name][:authorizations].each do |host, header|
58
+ prompts += "\t\t - `#{host}`: `#{header.keys.join("`, `")}`\n"
59
+ end
60
+ end
61
+
62
+ if prompts.length > 5000
21
63
  respond "*ChatGPT*: Session *#{session_name}*."
22
64
  send_file(Thread.current[:dest], "ChatGPT prompts for #{session_name}", "prompts.txt", "prompts", "text/plain", "text", content: prompts)
23
65
  elsif prompts.empty?
24
66
  respond "*ChatGPT*: Session *#{session_name}* has no prompts."
25
67
  else
26
68
  respond "*ChatGPT*: Session *#{session_name}*."
27
- if prompts.include?("`")
28
- respond prompts
29
- else
30
- respond "```#{prompts}```"
31
- end
69
+ respond transform_to_slack_markdown(prompts)
32
70
  end
33
71
  else
34
72
  respond "*ChatGPT*: You don't have a session with that name.\nCall `chatGPT list sessions` to see your saved sessions."
@@ -3,7 +3,7 @@ class SlackSmartBot
3
3
  module General
4
4
  module AI
5
5
  module OpenAI
6
- def open_ai_chat_list_sessions(type, tag: '') #type can be :own or :public or :shared
6
+ def open_ai_chat_list_sessions(type, tag: "") #type can be :own or :public or :shared
7
7
  save_stats(__method__)
8
8
 
9
9
  user = Thread.current[:user].dup
@@ -11,6 +11,7 @@ class SlackSmartBot
11
11
  team_id_user = Thread.current[:team_id_user]
12
12
 
13
13
  channel = Thread.current[:dest]
14
+ on_dm = channel[0] == "D"
14
15
 
15
16
  get_openai_sessions()
16
17
  check_users = []
@@ -19,60 +20,124 @@ class SlackSmartBot
19
20
  else
20
21
  check_users = @open_ai.keys
21
22
  end
23
+ members = {}
22
24
 
23
- list_sessions = []
25
+ list_sessions = {}
24
26
  check_users.each do |team_user_name|
25
27
  if @open_ai.key?(team_user_name) and @open_ai[team_user_name].key?(:chat_gpt) and
26
- @open_ai[team_user_name][:chat_gpt].key?(:sessions) and
27
- @open_ai[team_user_name][:chat_gpt][:sessions].size > 0
28
-
28
+ @open_ai[team_user_name][:chat_gpt].key?(:sessions) and
29
+ @open_ai[team_user_name][:chat_gpt][:sessions].size > 0
29
30
  sessions = @open_ai[team_user_name][:chat_gpt][:sessions].keys.sort
30
31
  sessions.delete("")
32
+
31
33
  sessions.each do |session_name|
32
34
  session = @open_ai[team_user_name][:chat_gpt][:sessions][session_name]
35
+ if type == :shared and on_dm and session.key?(:shared)
36
+ session[:shared].each do |channel_shared|
37
+ members[channel_shared] ||= get_channel_members(channel_shared)
38
+ end
39
+ end
33
40
  if (type == :own and session[:user_creator] == user.name) or
34
- (type == :public and session.key?(:public) and session[:public]) or
35
- (type == :shared and session.key?(:shared) and session[:shared].include?(channel))
36
-
37
- if tag == '' or (session.key?(:tag) and tag == session[:tag].to_s)
38
- if !session.key?(:team_creator) or session[:team_creator] == ''
39
- session[:team_creator] = config.team_id
41
+ (type == :public and session.key?(:public) and session[:public]) or
42
+ (type == :shared and session.key?(:shared) and session[:shared].include?(channel)) or
43
+ (type == :shared and session.key?(:shared) and on_dm and session[:shared].size > 0)
44
+ if tag == "" or (session.key?(:tag) and tag == session[:tag].to_s)
45
+ if type == :shared and on_dm
46
+ channels_check = []
47
+ session.shared.each do |channel_shared|
48
+ channels_check << channel_shared if members.key?(channel_shared) and !members[channel_shared].nil? and members[channel_shared].include?(user.id)
49
+ end
50
+ else
51
+ channels_check = [channel]
52
+ end
53
+ channels_check.each do |channel_check|
54
+ if !session.key?(:team_creator) or session[:team_creator] == ""
55
+ session[:team_creator] = config.team_id
56
+ end
57
+ #get the last context from prompts
58
+ team_user_creator = session[:team_creator] + "_" + session[:user_creator]
59
+ if !@ai_gpt.key?(team_user_creator) or !@ai_gpt[team_user_creator].key?(session_name)
60
+ get_openai_sessions(session_name, team_id: session[:team_creator], user_name: session[:user_creator])
61
+ end
62
+ if @ai_gpt.key?(team_user_creator) and @ai_gpt[team_user_creator].key?(session_name)
63
+ prompts_array = @ai_gpt[team_user_creator][session_name]
64
+ else
65
+ prompts_array = []
66
+ end
67
+ context = ""
68
+ if prompts_array.size > 0
69
+ prompts_array.reverse.each do |prompt|
70
+ if prompt[:role] == "system" and prompt[:content].size > 0 and prompt[:content][0][:type] == "text"
71
+ context = prompt[:content][0][:text]
72
+ break
73
+ end
74
+ end
75
+ end
76
+ list_session = []
77
+ list_session << "*`#{session_name}`*: "
78
+ list_session[-1] << "_#{session[:description]}_ " if session.key?(:description) and session[:description].to_s.strip != ""
79
+ list_session[-1] << "*(public)* " if session.key?(:public) and session[:public] and type != :public
80
+ list_session[-1] << "(shared on <##{session.shared.join(">, <#")}>) " if session.key?(:shared) and session[:shared].size > 0 and type != :shared
81
+ list_session[-1] << "\n\t:spiral_note_pad: *#{session.num_prompts}* prompts. "
82
+ list_session[-1] << " tag: >*#{session.tag}*. " if session.key?(:tag) and session[:tag].to_s != "" and tag == ""
83
+ list_session[-1] << "shared by: *#{session.user_creator}*. " if type != :own
84
+ list_session[-1] << "original creator: *#{session.copy_of_user}*. " if session.key?(:copy_of_user) and session[:copy_of_user] != "" and session[:copy_of_user] != session[:user_creator]
85
+ list_session[-1] << "model: #{session.model}. " if session.key?(:model) and session[:model] != ""
86
+ if on_dm and config.masters.include?(user.name)
87
+ list_session[-1] << "copies: #{session.users_copying.size}. " if session.key?(:users_copying) and session[:users_copying].size > 0
88
+ list_session[-1] << "users: #{session.users_copying.uniq.size}. " if session.key?(:users_copying) and session[:users_copying].size > 0
89
+ list_session[-1] << "temp copies: #{session.temp_copies}. " if session.key?(:temp_copies) and session[:temp_copies] > 0
90
+ list_session[-1] << "own temp copies: #{session.own_temp_copies}. " if session.key?(:own_temp_copies) and session[:own_temp_copies] > 0
91
+ else
92
+ num_copies = session.users_copying.size + session.temp_copies.to_i + session.own_temp_copies.to_i
93
+ list_session[-1] << "copies: #{num_copies}. " if num_copies > 0
94
+ end
95
+ list_session[-1] << "collaborators: *#{session.collaborators.join(", ").gsub("#{team_id}_", "")}*. " unless !session.key?(:collaborators) or session.collaborators.empty?
96
+ list_session[-1] << "last prompt: #{session.last_activity.gsub("-", "/")[0..15]}. " if type == :own
97
+ list_session[-1] << "\n\t:robot_face: context: #{context}" if context != ""
98
+ list_session[-1] << "\n\t:pushpin: static content:\n\t\t#{session.static_content.join("\n\t\t")}" if !session[:static_content].nil? and session[:static_content].size > 0
99
+ list_session[-1] << "\n\t:globe_with_meridians: live content:\n\t\t#{session.live_content.join("\n\t\t")}" if !session[:live_content].nil? and session[:live_content].size > 0
100
+ path_to_docs_folder_file = "#{config.path}/openai/#{session.team_creator}/#{session.user_creator}/#{session_name}/chatgpt_prompts.json"
101
+ if File.exist?(path_to_docs_folder_file)
102
+ num_docs = JSON.parse(File.read(path_to_docs_folder_file)).size
103
+ list_session[-1] << "\n\t:notebook_with_decorative_cover: #{num_docs} documents can be used in this session." if num_docs > 0
104
+ end
105
+ if session.key?(:authorizations) and session[:authorizations].size > 0
106
+ list_session[-1] << "\n\t:lock: authorizations:\n"
107
+ session[:authorizations].each do |key, value|
108
+ list_session[-1] << "\t\t#{key}\n"
109
+ end
110
+ end
111
+ list_sessions[channel_check] ||= []
112
+ list_sessions[channel_check] += list_session
40
113
  end
41
- list_sessions << "*`#{session_name}`*: "
42
- list_sessions[-1]<<"_#{session[:description]}_ " if session.key?(:description) and session[:description].to_s.strip != ''
43
- list_sessions[-1]<<"*(public)* " if session.key?(:public) and session[:public] and type != :public
44
- list_sessions[-1]<<"(shared on <##{session.shared.join(">, <#")}>) " if session.key?(:shared) and session[:shared].size > 0 and type != :shared
45
- list_sessions[-1]<<"\n *#{session.num_prompts}* prompts. "
46
- list_sessions[-1]<<" tag: >*#{session.tag}*. " if session.key?(:tag) and session[:tag].to_s != '' and tag == ''
47
- list_sessions[-1]<<"shared by: *#{session.user_creator}*. " if type != :own
48
- list_sessions[-1]<<"original creator: *#{session.copy_of_user}*. " if session.key?(:copy_of_user) and session[:copy_of_user] != '' and session[:copy_of_user] != session[:user_creator]
49
- list_sessions[-1]<<"model: #{session.model}. " if session.key?(:model) and session[:model] != ''
50
- list_sessions[-1]<<"copies: #{session.users_copying.size}. " if session.key?(:users_copying) and session[:users_copying].size > 0
51
- list_sessions[-1]<<"users: #{session.users_copying.uniq.size}. " if session.key?(:users_copying) and session[:users_copying].size > 0
52
- list_sessions[-1]<<"collaborators: *#{session.collaborators.join(", ").gsub("#{team_id}_","")}*. " unless !session.key?(:collaborators) or session.collaborators.empty?
53
- list_sessions[-1]<<"last prompt: #{session.last_activity.gsub("-", "/")[0..15]}. " if type == :own
54
114
  end
55
115
  end
56
116
  end
57
117
  end
58
118
  end
59
119
  if list_sessions.size > 0
60
- list_sessions[-1] << "\n\n:information_source: To start using a session: `chatgpt use USER_SHARED SESSION_NAME`" if type != :own
61
- if type == :own
62
- respond "*ChatGPT*: Your#{" >*#{tag}*" if tag!=''} sessions:\n\n#{list_sessions.join("\n\n")}"
63
- elsif type == :public
64
- respond "*ChatGPT*: Public#{" >*#{tag}*" if tag!=''} sessions:\n\n#{list_sessions.join("\n\n")}"
65
- elsif type == :shared
66
- respond "*ChatGPT*: Shared#{" >*#{tag}*" if tag!=''} sessions on <##{channel}>:\n\n#{list_sessions.join("\n\n")}"
120
+ list_sessions.each do |channel, list|
121
+ if type == :own
122
+ respond "*ChatGPT*: Your#{" >*#{tag}*" if tag != ""} sessions:\n\n#{list.join("\n\n")}", unfurl_links: false
123
+ elsif type == :public
124
+ respond "*ChatGPT*: Public#{" >*#{tag}*" if tag != ""} sessions on #{config.team_name}:\n\n#{list.join("\n\n")}", unfurl_links: false
125
+ elsif type == :shared
126
+ if channel[0] == "D" and @rules_imported[team_id_user].key?(user.name) and @rules_imported[team_id_user][user.name] != ""
127
+ respond "*ChatGPT*: Shared#{" >*#{tag}*" if tag != ""} sessions on <##{@rules_imported[team_id_user][user.name]}>:\n\n#{list.join("\n\n")}", unfurl_links: false
128
+ else
129
+ respond "*ChatGPT*: Shared#{" >*#{tag}*" if tag != ""} sessions on <##{channel}>:\n\n#{list.join("\n\n")}", unfurl_links: false
130
+ end
131
+ end
67
132
  end
133
+ respond "\n\n:information_source: To start using a session: `chatgpt use USER_SHARED SESSION_NAME` or `?? use SESSION_NAME PROMPT`" if type != :own
68
134
  else
69
135
  if type == :own
70
- respond "*ChatGPT*: You don't have any#{" >*#{tag}*" if tag!=''} sessions."
136
+ respond "*ChatGPT*: You don't have any#{" >*#{tag}*" if tag != ""} sessions."
71
137
  else
72
- respond "*ChatGPT*: There are no#{" >*#{tag}*" if tag!=''} #{type} sessions."
138
+ respond "*ChatGPT*: There are no#{" >*#{tag}*" if tag != ""} #{type} sessions."
73
139
  end
74
140
  end
75
-
76
141
  end
77
142
  end
78
143
  end
@@ -22,9 +22,19 @@ class SlackSmartBot
22
22
  end
23
23
  elsif type == :stop
24
24
  if channel_id == ""
25
- @open_ai[team_id_user][:chat_gpt][:sessions][session_name].public = false
25
+ if @open_ai[team_id_user][:chat_gpt][:sessions][session_name].public
26
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name].public = false
27
+ else
28
+ respond "*ChatGPT*: Session *#{session_name}* is not public."
29
+ return
30
+ end
26
31
  else
27
- @open_ai[team_id_user][:chat_gpt][:sessions][session_name].shared.delete(channel_id)
32
+ if @open_ai[team_id_user][:chat_gpt][:sessions][session_name].shared.include?(channel_id)
33
+ @open_ai[team_id_user][:chat_gpt][:sessions][session_name].shared.delete(channel_id)
34
+ else
35
+ respond "*ChatGPT*: Session *#{session_name}* is not shared on <##{channel_id}>."
36
+ return
37
+ end
28
38
  end
29
39
  end
30
40
  update_openai_sessions()
@@ -5,42 +5,53 @@ class SlackSmartBot
5
5
  module OpenAI
6
6
  def open_ai_chat_use_model(model, dont_save_stats: false)
7
7
  save_stats(__method__) unless dont_save_stats
8
- open_ai_models('', just_models: true) if @open_ai_models.empty?
9
- model_selected = @open_ai_models.select{|m| m==model}
10
- model_selected = @open_ai_models.select{|m| m.include?(model)} if model_selected.empty?
8
+ open_ai_models("", just_models: true) if @open_ai_models.empty?
9
+ model_selected = @open_ai_models.select { |m| m == model }
10
+ model_selected = @open_ai_models.select { |m| m.include?(model) } if model_selected.empty?
11
11
  if model_selected.size == 1
12
12
  model = model_selected[0]
13
13
  user = Thread.current[:user].dup
14
14
  team_id_user = Thread.current[:team_id_user]
15
15
 
16
16
  user_name = user.name
17
- if @chat_gpt_collaborating.key?(team_id_user) and @chat_gpt_collaborating[team_id_user].key?(Thread.current[:thread_ts])
18
- user_creator = @chat_gpt_collaborating[team_id_user][Thread.current[:thread_ts]][:user_creator]
19
- team_creator = @chat_gpt_collaborating[team_id_user][Thread.current[:thread_ts]][:team_creator]
20
- team_id_user_creator = team_creator + "_" + user_creator
21
- session_name = @chat_gpt_collaborating[team_id_user][Thread.current[:thread_ts]][:session_name]
22
- else
23
- user_creator = user.name
24
- team_creator = user.team_id
25
- team_id_user_creator = team_creator + "_" + user_creator
26
17
 
27
- @active_chat_gpt_sessions[team_id_user_creator] ||= {}
28
- if @active_chat_gpt_sessions[team_id_user_creator].key?(Thread.current[:thread_ts])
29
- session_name = @active_chat_gpt_sessions[team_id_user_creator][Thread.current[:thread_ts]]
30
- elsif @active_chat_gpt_sessions[team_id_user_creator].key?(Thread.current[:dest])
31
- session_name = @active_chat_gpt_sessions[team_id_user_creator][Thread.current[:dest]]
32
- else
33
- session_name = ''
18
+ restricted = false
19
+ if File.exist?("#{config.path}/openai/restricted_models.yaml")
20
+ restricted_models = YAML.load_file("#{config.path}/openai/restricted_models.yaml")
21
+ if restricted_models.key?(model) and !restricted_models[model].include?(team_id_user) and !restricted_models[model].include?(user_name)
22
+ respond "You don't have access to this model. You can request access to an admin user."
23
+ restricted = true
34
24
  end
35
25
  end
36
- if @open_ai.key?(team_id_user_creator) and @open_ai[team_id_user_creator].key?(:chat_gpt) and @open_ai[team_id_user_creator][:chat_gpt].key?(:sessions) and
37
- @open_ai[team_id_user_creator][:chat_gpt][:sessions].key?(session_name)
38
- @open_ai[team_id_user_creator][:chat_gpt][:sessions][session_name][:model] = model
39
- respond "Model for this session is now #{model}" unless dont_save_stats
40
- update_openai_sessions(session_name, team_id: team_creator, user_name: user_creator) unless session_name == ''
26
+ unless restricted
27
+ if @chat_gpt_collaborating.key?(team_id_user) and @chat_gpt_collaborating[team_id_user].key?(Thread.current[:thread_ts])
28
+ user_creator = @chat_gpt_collaborating[team_id_user][Thread.current[:thread_ts]][:user_creator]
29
+ team_creator = @chat_gpt_collaborating[team_id_user][Thread.current[:thread_ts]][:team_creator]
30
+ team_id_user_creator = team_creator + "_" + user_creator
31
+ session_name = @chat_gpt_collaborating[team_id_user][Thread.current[:thread_ts]][:session_name]
32
+ else
33
+ user_creator = user.name
34
+ team_creator = user.team_id
35
+ team_id_user_creator = team_creator + "_" + user_creator
36
+
37
+ @active_chat_gpt_sessions[team_id_user_creator] ||= {}
38
+ if @active_chat_gpt_sessions[team_id_user_creator].key?(Thread.current[:thread_ts])
39
+ session_name = @active_chat_gpt_sessions[team_id_user_creator][Thread.current[:thread_ts]]
40
+ elsif @active_chat_gpt_sessions[team_id_user_creator].key?(Thread.current[:dest])
41
+ session_name = @active_chat_gpt_sessions[team_id_user_creator][Thread.current[:dest]]
42
+ else
43
+ session_name = ""
44
+ end
45
+ end
46
+ if @open_ai.key?(team_id_user_creator) and @open_ai[team_id_user_creator].key?(:chat_gpt) and @open_ai[team_id_user_creator][:chat_gpt].key?(:sessions) and
47
+ @open_ai[team_id_user_creator][:chat_gpt][:sessions].key?(session_name)
48
+ @open_ai[team_id_user_creator][:chat_gpt][:sessions][session_name][:model] = model
49
+ respond "Model for this session is now #{model}" unless dont_save_stats
50
+ update_openai_sessions(session_name, team_id: team_creator, user_name: user_creator) unless session_name == ""
51
+ end
41
52
  end
42
53
  elsif model_selected.size > 1
43
- respond "There are more than one model with that name. Please be more specific: #{model_selected.join(', ')}"
54
+ respond "There are more than one model with that name. Please be more specific: #{model_selected.join(", ")}"
44
55
  else
45
56
  respond "There is no model with that name."
46
57
  end