-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathplugin.rb
277 lines (232 loc) · 11.1 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true
# name: discourse-chatbot
# about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages
# version: 1.4.6
# authors: merefield
# url: https://github.com/merefield/discourse-chatbot
gem 'multipart-post', '2.4.0', { require: false }
gem 'faraday-multipart', '1.0.4', { require: false }
gem 'event_stream_parser', '1.0.0', { require: false }
gem "ruby-openai", '7.0.0', { require: false }
# google search
gem "google_search_results", '2.2.0'
# wikipedia
gem "wikipedia-client", '1.17.0'
# safe ruby for calculations and date functions
gem "childprocess", "5.0.0"
# gem "safe_ruby", "1.0.4" TODO add this back in if gem returns to being maintained
module ::DiscourseChatbot
PLUGIN_NAME = "discourse-chatbot"
POST = "post"
MESSAGE = "message"
CHATBOT_QUERIES_CUSTOM_FIELD = "chatbot_queries"
CHATBOT_REMAINING_QUOTA_QUERIES_CUSTOM_FIELD = "chatbot_remanining_quota_queries"
CHATBOT_REMAINING_QUOTA_TOKENS_CUSTOM_FIELD = "chatbot_remaining_quota_tokens"
CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD = "chatbot_queries_quota_reach_escalation_date"
POST_TYPES_REGULAR_ONLY = [1]
POST_TYPES_INC_WHISPERS = [1, 4]
TRUST_LEVELS = ["low", "medium", "high"]
HIGH_TRUST_LEVEL = 3
MEDIUM_TRUST_LEVEL = 2
LOW_TRUST_LEVEL = 1
EMBEDDING_PROCESS_POSTS_CHUNK = 300
TOPIC_URL_REGEX = %r{\/t/[^/]+/(\d+)(?!\d|\/)}
POST_URL_REGEX = %r{\/t/[^/]+/(\d+)/(\d+)(?!\d|\/)}
NON_POST_URL_REGEX = %r{\bhttps?:\/\/[^\s\/$.?#].[^\s)]*}
REASONING_MODELS = ["o1-preview", "o1-mini"]
def progress_debug_message(message)
puts "Chatbot: #{message}" if SiteSetting.chatbot_enable_verbose_console_logging
if SiteSetting.chatbot_enable_verbose_rails_logging == "all"
case SiteSetting.chatbot_verbose_rails_logging_destination_level
when "warn"
Rails.logger.warn("Chatbot: #{message}")
else
Rails.logger.info("Chatbot: #{message}")
end
end
end
module_function :progress_debug_message
end
require_relative "lib/discourse_chatbot/engine"
enabled_site_setting :chatbot_enabled
register_asset 'stylesheets/common/chatbot_common.scss'
register_asset 'stylesheets/mobile/chatbot_mobile.scss', :mobile
register_svg_icon 'robot'
DiscoursePluginRegistry.serialized_current_user_fields << "chatbot_user_prefs_disable_quickchat_pm_composer_popup_mobile"
after_initialize do
# Allow user to disable quickchat Composer popup on mobile PMs
User.register_custom_field_type('chatbot_user_prefs_disable_quickchat_pm_composer_popup_mobile', :boolean)
register_editable_user_custom_field :chatbot_user_prefs_disable_quickchat_pm_composer_popup_mobile
Category.register_custom_field_type('chatbot_auto_response_additional_prompt', :string)
SeedFu.fixture_paths << Rails
.root
.join("plugins", "discourse-chatbot", "db", "fixtures")
.to_s
%w(
../lib/discourse_chatbot/event_evaluation.rb
../app/models/discourse_chatbot/post_embedding.rb
../app/models/discourse_chatbot/post_embeddings_bookmark.rb
../app/models/discourse_chatbot/topic_title_embedding.rb
../app/models/discourse_chatbot/topic_embeddings_bookmark.rb
../lib/discourse_chatbot/embedding_process.rb
../lib/discourse_chatbot/post/post_embedding_process.rb
../lib/discourse_chatbot/topic/topic_title_embedding_process.rb
../lib/discourse_chatbot/embedding_completionist_process.rb
../lib/discourse_chatbot/message/message_evaluation.rb
../lib/discourse_chatbot/post/post_evaluation.rb
../lib/discourse_chatbot/bot.rb
../lib/discourse_chatbot/bots/open_ai_bot_base.rb
../lib/discourse_chatbot/bots/open_ai_bot_basic.rb
../lib/discourse_chatbot/bots/open_ai_bot_rag.rb
../lib/discourse_chatbot/safe_ruby/lib/safe_ruby.rb
../lib/discourse_chatbot/function.rb
../lib/discourse_chatbot/functions/remaining_quota_function.rb
../lib/discourse_chatbot/functions/user_field_function.rb
../lib/discourse_chatbot/functions/calculator_function.rb
../lib/discourse_chatbot/functions/escalate_to_staff_function.rb
../lib/discourse_chatbot/functions/news_function.rb
../lib/discourse_chatbot/functions/web_crawler_function.rb
../lib/discourse_chatbot/functions/web_search_function.rb
../lib/discourse_chatbot/functions/wikipedia_function.rb
../lib/discourse_chatbot/functions/vision_function.rb
../lib/discourse_chatbot/functions/paint_function.rb
../lib/discourse_chatbot/functions/forum_search_function.rb
../lib/discourse_chatbot/functions/forum_user_distance_from_location_function.rb
../lib/discourse_chatbot/functions/forum_user_search_from_location_function.rb
../lib/discourse_chatbot/functions/forum_user_search_from_user_location_function.rb
../lib/discourse_chatbot/functions/forum_user_search_from_topic_location_function.rb
../lib/discourse_chatbot/functions/forum_get_user_address_function.rb
../lib/discourse_chatbot/functions/forum_topic_search_from_location_function.rb
../lib/discourse_chatbot/functions/forum_topic_search_from_user_location_function.rb
../lib/discourse_chatbot/functions/forum_topic_search_from_topic_location_function.rb
../lib/discourse_chatbot/functions/get_distance_between_locations_function.rb
../lib/discourse_chatbot/functions/coords_from_location_description_search.rb
../lib/discourse_chatbot/functions/stock_data_function.rb
../lib/discourse_chatbot/functions/parser.rb
../lib/discourse_chatbot/prompt_utils.rb
../lib/discourse_chatbot/post/post_prompt_utils.rb
../lib/discourse_chatbot/message/message_prompt_utils.rb
../lib/discourse_chatbot/reply_creator.rb
../lib/discourse_chatbot/post/post_reply_creator.rb
../lib/discourse_chatbot/message/message_reply_creator.rb
../app/controllers/discourse_chatbot/chatbot_controller.rb
../app/jobs/regular/chatbot_reply.rb
../app/jobs/regular/chatbot_post_embedding.rb
../app/jobs/regular/chatbot_post_embedding_delete.rb
../app/jobs/regular/chatbot_topic_title_embedding.rb
../app/jobs/regular/chatbot_topic_title_embedding_delete.rb
../app/jobs/scheduled/chatbot_quota_reset.rb
../app/jobs/scheduled/chatbot_embeddings_set_completer.rb
).each do |path|
load File.expand_path(path, __FILE__)
end
register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_CUSTOM_FIELD, :integer)
register_user_custom_field_type(::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_QUERIES_CUSTOM_FIELD, :integer)
register_user_custom_field_type(::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_TOKENS_CUSTOM_FIELD, :integer)
register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD, :date)
# Initialize Chatbot Quotas for all users as required
user_count = User.count
queries_field_count = UserCustomField.where(name: ::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_QUERIES_CUSTOM_FIELD).count
token_field_count = UserCustomField.where(name: ::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_TOKENS_CUSTOM_FIELD).count
pp "CHATBOT: Checking presence of Chatbot Custom Fields"
if user_count > queries_field_count * 2 || user_count > token_field_count * 2
pp "CHATBOT: Resetting Chatbot Quotas for all users as many users without required Chatbot Custom Fields"
::DiscourseChatbot::Bot.new.reset_all_quotas
end
add_to_serializer(:current_user, :chatbot_access) do
!::DiscourseChatbot::EventEvaluation.new.trust_level(object.id).blank?
end
#TODO this prevents a NotFound error in reads controller. This is a bit of a hack, we should really be finding the source of the issue and fixing it there
module ChatUpdateUserLastReadExtension
def fetch_active_membership(guardian:, channel:)
bot_user = ::User.find_by(username: SiteSetting.chatbot_bot_user)
bot_guardian = Guardian.new(bot_user)
bot_membership = ::Chat::ChannelMembershipManager.new(channel).find_for_user(bot_guardian.user)
if bot_membership.nil?
membership = ::Chat::ChannelMembershipManager.new(channel).find_for_user(guardian.user, following: true)
else
membership = ::Chat::ChannelMembershipManager.new(channel).find_for_user(guardian.user)
end
membership
end
end
class ::Chat::UpdateUserLastRead
prepend ChatUpdateUserLastReadExtension
end
DiscourseEvent.on(:post_created) do |*params|
post, opts, user = params
if SiteSetting.chatbot_enabled
if post.post_type == 1
job_class = ::Jobs::ChatbotPostEmbedding
job_class.perform_async({id: post.id}.stringify_keys)
end
if (post.post_type == 1 || post.post_type == 4 && SiteSetting.chatbot_can_trigger_from_whisper)
::DiscourseChatbot.progress_debug_message("1. trigger")
bot_username = SiteSetting.chatbot_bot_user
bot_user = User.find_by(username: bot_username)
if bot_user && (user.id != bot_user.id)
event_evaluation = ::DiscourseChatbot::PostEvaluation.new
event_evaluation.on_submission(post)
end
end
end
end
DiscourseEvent.on(:topic_destroyed) do |*params|
topic, opts, user = params
if SiteSetting.chatbot_enabled
job_class = ::Jobs::ChatbotTopicTitleEmbeddingDelete
job_class.perform_async({id: topic.id}.stringify_keys)
end
end
DiscourseEvent.on(:topic_recovered) do |*params|
topic, opts = params
if SiteSetting.chatbot_enabled
job_class = ::Jobs::ChatbotTopicTitleEmbedding
job_class.perform_async({id: topic.id}.stringify_keys)
end
end
DiscourseEvent.on(:topic_created) do |*params|
topic, opts = params
if SiteSetting.chatbot_enabled
job_class = ::Jobs::ChatbotTopicTitleEmbedding
job_class.perform_async({id: topic.id}.stringify_keys)
end
end
DiscourseEvent.on(:post_edited) do |*params|
post, topic_changed, opts = params
if SiteSetting.chatbot_enabled && post.post_type == 1
job_class = ::Jobs::ChatbotPostEmbedding
job_class.perform_async({id: post.id}.stringify_keys)
if post.is_first_post? && topic_changed
job_class = ::Jobs::ChatbotTopicTitleEmbedding
job_class.perform_async({id: post.topic.id}.stringify_keys)
end
end
end
DiscourseEvent.on(:post_recovered) do |*params|
post, opts = params
if SiteSetting.chatbot_enabled && post.post_type == 1
job_class = ::Jobs::ChatbotPostEmbedding
job_class.perform_async({id: post.id}.stringify_keys)
end
end
DiscourseEvent.on(:post_destroyed) do |*params|
post, opts, user = params
if SiteSetting.chatbot_enabled && post.post_type == 1
job_class = ::Jobs::ChatbotPostEmbeddingDelete
job_class.perform_async({id: post.id}.stringify_keys)
end
end
DiscourseEvent.on(:chat_message_created) do |*params|
chat_message, chat_channel, user = params
if SiteSetting.chatbot_enabled
::DiscourseChatbot.progress_debug_message("1. trigger")
bot_username = SiteSetting.chatbot_bot_user
bot_user = User.find_by(username: bot_username)
if bot_user && (user.id != bot_user.id)
event_evaluation = ::DiscourseChatbot::MessageEvaluation.new
event_evaluation.on_submission(chat_message)
end
end
end
end