-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathplugin.rb
More file actions
382 lines (328 loc) · 12.7 KB
/
Copy pathplugin.rb
File metadata and controls
382 lines (328 loc) · 12.7 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# frozen_string_literal: true
# name: discourse-chatbot
# about: a plugin that allows you to have a conversation with a configurable chatbot in Chat, Topics and Private Messages
# version: 1.7.5
# authors: merefield
# url: https://github.com/merefield/discourse-chatbot
gem "domain_name", "0.6.20240107", { require: false }
gem "http-cookie", "1.0.8", { require: false }
gem "event_stream_parser", "1.0.0", { require: false }
gem "ruby-openai", "8.1.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"
CHATBOT_LAST_ESCALATION_DATE_CUSTOM_FIELD = "chatbot_last_escalation_date"
CHATBOT_LAST_ESCALATION_TOPIC_ID_CUSTOM_FIELD =
"chatbot_last_escalation_topic_id"
POST_TYPES_REGULAR_ONLY = [1]
POST_TYPES_INC_WHISPERS = [1, 4]
TRUST_LEVELS = %w[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 = %w[
o1
o1-mini
o3
o3-mini
o4-mini
gpt-5.4
gpt-5.4-mini
gpt-5.4-nano
gpt-5
gpt-5-mini
gpt-5-nano
gpt-5.1
gpt-5.2
gpt-5.2-pro
]
def latest_chatbot_custom_field_values(user_id, name)
UserCustomField
.where(user_id: user_id, name: name)
.order(id: :desc)
.pluck(:value)
end
def latest_chatbot_custom_field_value(user_id, name)
latest_chatbot_custom_field_values(user_id, name).first
end
def latest_chatbot_escalation_topic_id(user_id)
values =
latest_chatbot_custom_field_values(
user_id,
CHATBOT_LAST_ESCALATION_TOPIC_ID_CUSTOM_FIELD
)
values.each do |value|
next if value.blank?
next unless value.to_s.match?(/\A\d+\z/)
return value.to_i
end
nil
end
def latest_chatbot_escalation_at(user_id)
values =
latest_chatbot_custom_field_values(
user_id,
CHATBOT_LAST_ESCALATION_DATE_CUSTOM_FIELD
)
values.each do |value|
next if value.blank?
parsed_value = Time.zone.parse(value)
return parsed_value if parsed_value
rescue ArgumentError, TypeError
next
end
nil
end
def chatbot_escalation_cooldown_elapsed?(user_id, now: Time.zone.now)
last_escalation_at = latest_chatbot_escalation_at(user_id)
return true if last_escalation_at.nil?
now >=
(
last_escalation_at +
SiteSetting.chatbot_escalate_to_staff_cool_down_period.days
)
end
def progress_debug_message(message)
if SiteSetting.chatbot_enable_verbose_console_logging
puts "Chatbot: #{message}"
end
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 :latest_chatbot_custom_field_values
module_function :latest_chatbot_custom_field_value
module_function :latest_chatbot_escalation_topic_id
module_function :latest_chatbot_escalation_at
module_function :chatbot_escalation_cooldown_elapsed?
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
register_editable_user_custom_field :chatbot_additional_prompt
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/paint_edit_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 { |path| load File.expand_path(path, __FILE__) }
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
)
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
Jobs.enqueue(:backfill_chatbot_quotas)
end