diff --git a/app/jobs/regular/update_ai_bot_pm_title.rb b/app/jobs/regular/update_ai_bot_pm_title.rb index 5da82291..0e4be014 100644 --- a/app/jobs/regular/update_ai_bot_pm_title.rb +++ b/app/jobs/regular/update_ai_bot_pm_title.rb @@ -12,6 +12,13 @@ module ::Jobs return unless post.topic.custom_fields[DiscourseAi::AiBot::EntryPoint::REQUIRE_TITLE_UPDATE] DiscourseAi::AiBot::Playground.new(bot).title_playground(post) + + publish_update(post.topic, { title: post.topic.title }) + end + + def publish_update(topic, payload) + allowed_users = topic.topic_allowed_users.pluck(:user_id) + MessageBus.publish("/discourse-ai/ai-bot/topic/#{topic.id}", payload, user_ids: allowed_users) end end end diff --git a/spec/lib/modules/ai_bot/jobs/regular/update_ai_bot_pm_title_spec.rb b/spec/lib/modules/ai_bot/jobs/regular/update_ai_bot_pm_title_spec.rb index d670b0dd..788d75a8 100644 --- a/spec/lib/modules/ai_bot/jobs/regular/update_ai_bot_pm_title_spec.rb +++ b/spec/lib/modules/ai_bot/jobs/regular/update_ai_bot_pm_title_spec.rb @@ -39,4 +39,27 @@ RSpec.describe Jobs::UpdateAiBotPmTitle do expect(post.reload.topic.title).to eq("My amazing title") end end + + it "will post an update with the new title to the message bus channel" do + SiteSetting.ai_bot_allowed_groups = Group::AUTO_GROUPS[:staff] + post = + create_post( + user: user, + raw: "Hello there", + title: "does not matter should be updated", + archetype: Archetype.private_message, + target_usernames: bot_user.username, + ) + title_result = "A great title would be:\n\nMy amazing title\n\n" + + DiscourseAi::Completions::Llm.with_prepared_responses([title_result]) do + messages = + MessageBus.track_publish("/discourse-ai/ai-bot/topic/#{post.topic.id}") do + subject.execute(bot_user_id: bot_user.id, post_id: post.id) + end + + final_update = messages.last.data + expect(final_update[:title]).to eq("My amazing title") + end + end end