DEV: Publish AI Bot PM title update to message bus channel (#781)

This commit is contained in:
Keegan George 2024-08-29 14:48:44 -07:00 committed by GitHub
parent 41054c4fb8
commit 94f6c632bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -12,6 +12,13 @@ module ::Jobs
return unless post.topic.custom_fields[DiscourseAi::AiBot::EntryPoint::REQUIRE_TITLE_UPDATE] return unless post.topic.custom_fields[DiscourseAi::AiBot::EntryPoint::REQUIRE_TITLE_UPDATE]
DiscourseAi::AiBot::Playground.new(bot).title_playground(post) 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 end
end end

View File

@ -39,4 +39,27 @@ RSpec.describe Jobs::UpdateAiBotPmTitle do
expect(post.reload.topic.title).to eq("My amazing title") expect(post.reload.topic.title).to eq("My amazing title")
end end
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 end