From 176a4458f2dac9206f96d8b04f035f83ed430e4e Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Thu, 7 Mar 2024 16:14:17 -0300 Subject: [PATCH] FIX: Prevent AI chat thread titles from being created before replies are posted (#517) Chat thread replies draft trigger the thread_created event, which we relied on to trigger the AI generated title. Because of that we now will use the noisier chat_message_created event, and manually check for thread and replies existence. See https://github.com/discourse/discourse/pull/26033 --- lib/ai_helper/entry_point.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/ai_helper/entry_point.rb b/lib/ai_helper/entry_point.rb index 840d2b55..14853946 100644 --- a/lib/ai_helper/entry_point.rb +++ b/lib/ai_helper/entry_point.rb @@ -10,14 +10,23 @@ module DiscourseAi additional_icons = %w[spell-check language images] additional_icons.each { |icon| plugin.register_svg_icon(icon) } - plugin.on(:chat_thread_created) do |thread| + plugin.on(:chat_message_created) do |message, channel, user, extra| next unless SiteSetting.composer_ai_helper_enabled next unless SiteSetting.ai_helper_automatic_chat_thread_title - ::Jobs.enqueue_in( - SiteSetting.ai_helper_automatic_chat_thread_title_delay.minutes, - :generate_chat_thread_title, - thread_id: thread.id, - ) + next unless extra[:thread].present? + next unless extra[:thread].title.blank? + + reply_count = extra[:thread].replies.count + + if reply_count.between?(1, 4) + ::Jobs.enqueue_in( + SiteSetting.ai_helper_automatic_chat_thread_title_delay.minutes, + :generate_chat_thread_title, + thread_id: extra[:thread].id, + ) + elsif reply_count >= 5 + ::Jobs.enqueue(:generate_chat_thread_title, thread_id: extra[:thread].id) + end end plugin.add_to_serializer(