From 94f6c632bf9763248b78c40ad0b0df66fc863749 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Thu, 29 Aug 2024 14:48:44 -0700 Subject: [PATCH] DEV: Publish AI Bot PM title update to message bus channel (#781) --- app/jobs/regular/update_ai_bot_pm_title.rb | 7 ++++++ .../regular/update_ai_bot_pm_title_spec.rb | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) 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