Skip to content

Commit 6ad214e

Browse files
add slack reply setting (#228)
Co-authored-by: Vijay Swamidass <[email protected]>
1 parent a214453 commit 6ad214e

File tree

6 files changed

+358
-36
lines changed

6 files changed

+358
-36
lines changed

app/models/message.rb

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,75 @@ class Message < ApplicationRecord
1414
private
1515

1616
def create_slack_post
17-
channel_id = chat.assistant.slack_channel_name.presence || chat.slack_channel_id
18-
19-
# Skip if there is already a thread linked or the assistant doesn't have a slack channel
20-
return if channel_id.blank?
21-
22-
# If the assistant is generating, then it isn't ready and we don't post to slack
23-
return unless ready?
24-
25-
# skip if this message already has a slack ts
26-
return if slack_ts
17+
return unless should_post_to_slack?
2718

2819
begin
29-
slack_service = SlackService.new
30-
31-
# if the chat.slack_thread is missing, we create a new thread
32-
self.slack_ts = slack_service.post_message(channel_id, content, chat.slack_thread, assistant?)
33-
34-
# if the chat didn't have a thread, save it.
35-
if chat.slack_thread.nil?
36-
chat.slack_thread = slack_ts
37-
chat.save
38-
end
39-
40-
if slack_ts
41-
slack_service.add_reaction(channel: chat.assistant.slack_channel_name, timestamp: slack_ts, emoji: 'pagerduty') if chat.webhook && (chat.webhook.hook_type == 'pagerduty')
42-
else
43-
Rails.logger.error("Failed to create Slack thread for chat ID: #{chat.id}")
44-
end
20+
post_message_to_slack
21+
add_pagerduty_reaction_if_needed
4522
rescue StandardError => e
46-
Rails.logger.error("Error in create_slack_post for message #{id}: #{e.message}")
47-
Rails.logger.error(e.backtrace.join("\n"))
48-
# Update the message content with the error information, bypassing callbacks
49-
error_message = "⚠️ Slack Error: #{e.message}"
50-
51-
# There may be a better place to do this, but will look at that later
52-
update_column(:content, "#{content}\n\n#{error_message}")
53-
# Don't raise the error to prevent the message creation from failing
23+
handle_slack_error(e)
5424
end
5525
end
5626

27+
def should_post_to_slack?
28+
return false if slack_channel_id.blank?
29+
return false unless ready?
30+
return false if slack_ts.present?
31+
return false if assistant_reply_only_mode_violated?
32+
33+
true
34+
end
35+
36+
def slack_channel_id
37+
@slack_channel_id ||= chat.assistant.slack_channel_name.presence || chat.slack_channel_id
38+
end
39+
40+
def assistant_reply_only_mode_violated?
41+
# If assistant is set to reply only and this is an assistant message without an existing thread,
42+
# it means we're trying to start a new conversation which violates reply-only mode
43+
chat.assistant.slack_reply_only? && assistant? && chat.slack_thread.blank?
44+
end
45+
46+
def post_message_to_slack
47+
slack_service = SlackService.new
48+
self.slack_ts = slack_service.post_message(slack_channel_id, content, chat.slack_thread, assistant?)
49+
50+
update_chat_thread_if_needed
51+
log_error_if_post_failed
52+
end
53+
54+
def update_chat_thread_if_needed
55+
return unless chat.slack_thread.nil? && slack_ts.present?
56+
57+
chat.update!(slack_thread: slack_ts)
58+
end
59+
60+
def log_error_if_post_failed
61+
return if slack_ts.present?
62+
63+
Rails.logger.error("Failed to create Slack thread for chat ID: #{chat.id}")
64+
end
65+
66+
def add_pagerduty_reaction_if_needed
67+
return unless slack_ts.present?
68+
return unless chat.webhook&.hook_type == 'pagerduty'
69+
70+
SlackService.new.add_reaction(
71+
channel: chat.assistant.slack_channel_name,
72+
timestamp: slack_ts,
73+
emoji: 'pagerduty'
74+
)
75+
end
76+
77+
def handle_slack_error(error)
78+
Rails.logger.error("Error in create_slack_post for message #{id}: #{error.message}")
79+
Rails.logger.error(error.backtrace.join("\n"))
80+
81+
# Consider using a separate error tracking field instead of modifying content
82+
error_message = "⚠️ Slack Error: #{error.message}"
83+
update_column(:content, "#{content}\n\n#{error_message}")
84+
end
85+
5786
def enqueue_generate_message_response_job
5887
return unless user?
5988

app/views/assistants/_assistant.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
<strong class="block text-lg font-semibold text-gray-800 mb-2">Enable Channel Join Message</strong>
6464
<p class="text-gray-700"><%= assistant.enable_channel_join_message ? 'Yes' : 'No' %></p>
6565
</div>
66+
<div class="my-5">
67+
<strong class="block text-lg font-semibold text-gray-800 mb-2">Slack Reply Only</strong>
68+
<p class="text-gray-700"><%= assistant.slack_reply_only ? 'Yes' : 'No' %></p>
69+
</div>
6670
<!-- Document Creation -->
6771
<div class="my-5">
6872
<strong class="block text-lg font-semibold text-gray-800 mb-2">Create Document on Approval</strong>

app/views/assistants/_form.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
<p id="" class="text-gray-500 text-xs">Post an initial message when the bot is added to the channel.</p>
8989
<%= form.check_box :enable_channel_join_message, class: "block shadow rounded-md border border-gray-400 outline-none mt-2" %>
9090
</div>
91+
<div class="my-5">
92+
<%= form.label "Slack Reply Only" %>
93+
<p id="" class="text-gray-500 text-xs">When enabled, the assistant will only respond to direct mentions in Slack threads. It will not post new threads in the channel.</p>
94+
<%= form.check_box :slack_reply_only, class: "block shadow rounded-md border border-gray-400 outline-none mt-2" %>
95+
</div>
9196
<% end %>
9297
<%= render "shared/form_section", title: "Document Creation (Optional)" do %>
9398
<div class="my-5">

spec/factories/assistants.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
libraries { '1' }
66
input { 'some input' }
77
name { 'Test Assistant' }
8+
instructions { 'Sample instructions' }
9+
output { 'Sample output' }
10+
slack_reply_only { false }
811
association :user
912
end
1013
end

spec/factories/messages.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
FactoryBot.define do
22
factory :message do
3-
chat { nil }
3+
association :chat
4+
association :user
45
content { "MyText" }
5-
from { 1 }
6+
from { :user }
7+
status { :ready }
68
end
79
end

0 commit comments

Comments
 (0)