@@ -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
0 commit comments