From 59bab2bba3bcaa9ca7fdc4edbf8eaafe422213ef Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 1 Mar 2024 07:53:42 +1100 Subject: [PATCH] FIX: stream messages when directly PMing a persona (#500) previous to this fix we did not consider personas a bot in the front end --- .../initializers/ai-bot-replies.js | 4 +++- lib/ai_bot/entry_point.rb | 8 +++++++ spec/lib/modules/ai_bot/entry_point_spec.rb | 21 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/initializers/ai-bot-replies.js b/assets/javascripts/initializers/ai-bot-replies.js index e1827733..9e8c875a 100644 --- a/assets/javascripts/initializers/ai-bot-replies.js +++ b/assets/javascripts/initializers/ai-bot-replies.js @@ -10,8 +10,9 @@ import copyConversation from "../discourse/lib/copy-conversation"; const AUTO_COPY_THRESHOLD = 4; +let enabledChatBotIds = []; function isGPTBot(user) { - return user && [-110, -111, -112, -113, -114, -115, -116].includes(user.id); + return user && enabledChatBotIds.includes(user.id); } function attachHeaderIcon(api) { @@ -197,6 +198,7 @@ export default { const user = container.lookup("service:current-user"); if (user?.ai_enabled_chat_bots) { + enabledChatBotIds = user.ai_enabled_chat_bots.map((bot) => bot.id); if (settings.ai_bot_add_to_header) { withPluginApi("1.6.0", attachHeaderIcon); } diff --git a/lib/ai_bot/entry_point.rb b/lib/ai_bot/entry_point.rb index 5dc846e4..a30b616e 100644 --- a/lib/ai_bot/entry_point.rb +++ b/lib/ai_bot/entry_point.rb @@ -97,6 +97,14 @@ module DiscourseAi SQL bots.each { |hash| hash["model_name"] = model_map[hash["id"]] } + mentionables = AiPersona.mentionables(user: scope.user) + if mentionables.present? + bots.concat( + mentionables.map do |mentionable| + { "id" => mentionable[:user_id], "username" => mentionable[:username] } + end, + ) + end bots end diff --git a/spec/lib/modules/ai_bot/entry_point_spec.rb b/spec/lib/modules/ai_bot/entry_point_spec.rb index b3326a54..ed9c38ce 100644 --- a/spec/lib/modules/ai_bot/entry_point_spec.rb +++ b/spec/lib/modules/ai_bot/entry_point_spec.rb @@ -23,6 +23,27 @@ RSpec.describe DiscourseAi::AiBot::EntryPoint do bot_allowed_group.add(admin) end + it "adds mentionables to current_user_serializer" do + Group.refresh_automatic_groups! + + persona = + Fabricate( + :ai_persona, + mentionable: true, + enabled: true, + allowed_group_ids: [bot_allowed_group.id], + ) + persona.create_user! + + serializer = CurrentUserSerializer.new(admin, scope: Guardian.new(admin)) + serializer = serializer.as_json + bots = serializer[:current_user][:ai_enabled_chat_bots] + + persona_bot = bots.find { |bot| bot["id"] == persona.user_id } + + expect(persona_bot["username"]).to eq(persona.user.username) + end + it "queues a job to generate a reply by the AI" do expect { PostCreator.create!(admin, post_args) }.to change( Jobs::CreateAiReply.jobs,