A platform for community discussion. Free, open, simple.
FIX: Correct AI model routing, Chat labels and bot navigation (#44403)
Natalie Tay3f9b5d850296
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-message.gjsindex 2ad34945184..c87ee7d5bef 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-message.gjs@@ -10,6 +10,8 @@ import { cancel, next, schedule } from "@ember/runloop";import { service } from "@ember/service";import { modifier } from "ember-modifier";import EmojiPicker from "discourse/components/emoji-picker";+import PluginOutlet from "discourse/components/plugin-outlet";+import lazyHash from "discourse/helpers/lazy-hash";import discourseDebounce from "discourse/lib/debounce";import { bind } from "discourse/lib/decorators";import getURL from "discourse/lib/get-url";@@ -788,6 +790,11 @@ export default class ChatMessage extends Component {@message={{@message}}/>+ <PluginOutlet+ @name="chat-message-after"+ @outletArgs={{lazyHash message=@message}}+ />+{{#if this.shouldRenderReactions}}<div class="chat-message-reaction-list">{{#each @message.reactions as |reaction|}}diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-channel-subscription-manager.js b/plugins/chat/assets/javascripts/discourse/lib/chat-channel-subscription-manager.jsindex 3b2ef556e9e..979b5e78e13 100644--- a/plugins/chat/assets/javascripts/discourse/lib/chat-channel-subscription-manager.js+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-channel-subscription-manager.js@@ -158,6 +158,7 @@ export default class ChatChannelSubscriptionManager {message.uploads = cloneJSON(data.chat_message.uploads || []);message.edited = data.chat_message.edited;message.streaming = data.chat_message.streaming;+ message.aiLlmName = data.chat_message.ai_llm_name;message.blocks = data.chat_message.blocks;message.isAction = data.chat_message.is_action;}diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-channel-thread-subscription-manager.js b/plugins/chat/assets/javascripts/discourse/lib/chat-channel-thread-subscription-manager.jsindex b7ab3d9677d..0cfb9cc7029 100644--- a/plugins/chat/assets/javascripts/discourse/lib/chat-channel-thread-subscription-manager.js+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-channel-thread-subscription-manager.js@@ -142,6 +142,7 @@ export default class ChatChannelThreadSubscriptionManager {message.uploads = cloneJSON(data.chat_message.uploads || []);message.edited = data.chat_message.edited;message.streaming = data.chat_message.streaming;+ message.aiLlmName = data.chat_message.ai_llm_name;message.blocks = data.chat_message.blocks;message.isAction = data.chat_message.is_action;}diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-message.js b/plugins/chat/assets/javascripts/discourse/models/chat-message.jsindex 5f74d883767..1b2b6d47a89 100644--- a/plugins/chat/assets/javascripts/discourse/models/chat-message.js+++ b/plugins/chat/assets/javascripts/discourse/models/chat-message.js@@ -66,6 +66,7 @@ export default class ChatMessage {@tracked pinned;@tracked isAction;@tracked blocks;+ @tracked aiLlmName;@autoTrackedArray reactions;@tracked _deletedAt;@@ -110,6 +111,7 @@ export default class ChatMessage {this.bookmark = args.bookmark ? Bookmark.create(args.bookmark) : null;this.mentionedUsers = this.#initMentionedUsers(args.mentioned_users);this.blocks = args.blocks;+ this.aiLlmName = args.ai_llm_name;this.threadTitle = args.thread_title;this.threadId = args.thread_id;diff --git a/plugins/chat/test/javascripts/unit/lib/chat-subscription-manager-test.js b/plugins/chat/test/javascripts/unit/lib/chat-subscription-manager-test.jsindex ab3ced81f40..c53df6d5da4 100644--- a/plugins/chat/test/javascripts/unit/lib/chat-subscription-manager-test.js+++ b/plugins/chat/test/javascripts/unit/lib/chat-subscription-manager-test.js@@ -23,6 +23,41 @@ module("Unit | Lib | chat subscription managers", function (hooks) {sinon.restore();});+ test("channel manager updates model attribution when streaming finishes", function (assert) {+ const channel = this.fabricators.channel();+ const message = this.fabricators.message({ channel, streaming: true });+ channel.messagesManager.addMessages([message]);+ const manager = new ChatChannelSubscriptionManager(this, channel);++ manager.handleEditMessage({+ chat_message: {+ id: message.id,+ streaming: false,+ ai_llm_name: "Model 猫",+ },+ });++ assert.strictEqual(message.aiLlmName, "Model 猫");+ });++ test("thread manager updates model attribution when streaming finishes", function (assert) {+ const channel = this.fabricators.channel();+ const thread = this.fabricators.thread({ channel });+ const message = this.fabricators.message({ channel, streaming: true });+ thread.messagesManager.addMessages([message]);+ const manager = new ChatChannelThreadSubscriptionManager(this, thread);++ manager.handleEditMessage({+ chat_message: {+ id: message.id,+ streaming: false,+ ai_llm_name: "Model 猫",+ },+ });++ assert.strictEqual(message.aiLlmName, "Model 猫");+ });+test("channel manager syncs uploads when matching staged message is sent", function (assert) {const channel = this.fabricators.channel({ id: 11 });const manager = new ChatChannelSubscriptionManager(this, channel);diff --git a/plugins/chat/test/javascripts/unit/models/chat-message-test.js b/plugins/chat/test/javascripts/unit/models/chat-message-test.jsindex f149c099654..ebd5e3aa28e 100644--- a/plugins/chat/test/javascripts/unit/models/chat-message-test.js+++ b/plugins/chat/test/javascripts/unit/models/chat-message-test.js@@ -7,6 +7,16 @@ import ChatMessage from "discourse/plugins/chat/discourse/models/chat-message";module("Unit | Models | chat-message", function (hooks) {setupTest(hooks);+ test("preserves model attribution from the server", function (assert) {+ const channel = new ChatFabricators(getOwner(this)).channel();+ const message = ChatMessage.create(channel, {+ id: 1,+ ai_llm_name: "Model 猫",+ });++ assert.strictEqual(message.aiLlmName, "Model 猫");+ });+test(".persisted", function (assert) {const channel = new ChatFabricators(getOwner(this)).channel();let message = ChatMessage.create(channel, { id: null });diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjsindex bedd00e416e..fab6799a8a7 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-header-icon.gjs@@ -16,6 +16,13 @@ export default class AiBotHeaderIcon extends Component {@service aiConversationsSidebarManager;get bots() {+ if (+ !this.siteSettings.ai_bot_enabled ||+ !this.currentUser?.ai_available_llm_models+ ) {+ return [];+ }+const hasSelectableModel = this.currentUser.ai_available_llm_models?.length;return (this.currentUser.ai_enabled_agents || []).filter((agent) =>diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-chat-model-label.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-chat-model-label.gjsnew file mode 100644index 00000000000..3f8de7427a5--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-chat-model-label.gjs@@ -0,0 +1,7 @@+export default <template>+ {{#if @outletArgs.message.aiLlmName}}+ <span class="ai-chat-message__model">+ {{@outletArgs.message.aiLlmName}}+ </span>+ {{/if}}+</template>diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/add-ai-bot-to-community-section.gjs b/plugins/discourse-ai/assets/javascripts/discourse/initializers/add-ai-bot-to-community-section.gjsindex f9760ba4644..1f1eac60004 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/add-ai-bot-to-community-section.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/add-ai-bot-to-community-section.gjs@@ -10,6 +10,8 @@ export default {if (!currentUser ||+ !siteSettings.ai_bot_enabled ||+ !currentUser.ai_available_llm_models ||!siteSettings.ai_bot_add_to_community_section ||!currentUser.ai_enabled_agents?.length) {diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.jsindex 9b8c0445bf2..6c521b963af 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js@@ -1,6 +1,7 @@import { withPluginApi } from "discourse/lib/plugin-api";import AiBotDockedComposer from "../components/ai-bot-docked-composer";import AiBotHeaderIcon from "../components/ai-bot-header-icon";+import AiChatModelLabel from "../components/ai-chat-model-label";import AiAgentFlair from "../components/post/ai-agent-flair";import AiCancelStreaming from "../components/post/meta-data/ai-cancel-streaming";import AiCancelStreamingButton from "../components/post-menu/ai-cancel-streaming-button";@@ -280,26 +281,6 @@ function initializeFooterButtonsVisibility(api) {);}-function initializeChatModelAttribution(api) {- if (!api.decorateChatMessage) {- return;- }-- api.decorateChatMessage((element, _helper, message) => {- if (- !message?.ai_llm_name ||- element.querySelector(".ai-chat-message__model")- ) {- return;- }-- const label = document.createElement("span");- label.classList.add("ai-chat-message__model");- label.textContent = message.ai_llm_name;- element.append(label);- });-}-export default {name: "discourse-ai-bot-replies",@@ -318,7 +299,7 @@ export default {api.serializeOnCreate("ai_llm_model_id", "aiLlmModelId");attachHeaderIcon(api);initializeAIBotReplies(api);- initializeChatModelAttribution(api);+ api.renderInOutlet("chat-message-after", AiChatModelLabel);initializeDebugButton(api, container);initializeShareButton(api, container);initializeFooterButtonsVisibility(api);diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex 995d1aa4c96..b73a4c6484f 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -270,7 +270,6 @@ module DiscourseAimodality: :automation,agent_id:,llm_model_id:,- topic: post.topic,speaker: user,selection_source: :snapshot,allow_general_fallback: false,@@ -308,6 +307,11 @@ module DiscourseAimodel_user_ids = participant_ids & LlmModel.with_user.pluck(:user_id)recipient_ids = (agent_user_ids + model_user_ids).uniq+ preferred_user_id = topic.custom_fields[BOT_USER_PREF_ID_CUSTOM_FIELD].to_i+ if agent_user_ids.empty? && model_user_ids.include?(preferred_user_id)+ return User.find_by(id: preferred_user_id)+ end+if recipient_ids.many?key = agent_user_ids.any? ? "ambiguous_agent" : "ambiguous_model"raise ConversationRoute::Error.new(diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rbindex a765d5f3dfc..38e59511447 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb@@ -2186,6 +2186,49 @@ RSpec.describe DiscourseAi::AiBot::Playground doenddescribe ".legacy_recipient_for" do+ it "rejects an ambiguous conversation when the preferred bot is no longer a participant" do+ first_model_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)+ second_model_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)+ former_model_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)+ claude_2.update_column(:user_id, first_model_user.id)+ opus_model.update_column(:user_id, second_model_user.id)+ Fabricate(:llm_model).update_column(:user_id, former_model_user.id)+ topic = Fabricate(:private_message_topic, user:, recipient: first_model_user)+ topic.topic_allowed_users.create!(user: second_model_user)+ topic.custom_fields[described_class::BOT_USER_PREF_ID_CUSTOM_FIELD] = former_model_user.id+ topic.save_custom_fields++ expect { described_class.legacy_recipient_for(topic, []) }.to raise_error(+ DiscourseAi::AiBot::ConversationRoute::Error,+ I18n.t("discourse_ai.ai_bot.errors.ambiguous_model"),+ )+ end++ it "continues a legacy conversation with its preferred model recipient" do+ first_model_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)+ preferred_model_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)+ claude_2.update_column(:user_id, first_model_user.id)+ opus_model.update_column(:user_id, preferred_model_user.id)+ toggle_enabled_bots(bots: [claude_2, opus_model])+ SiteSetting.ai_bot_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]+ general_agent.update!(allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]])+ topic = Fabricate(:private_message_topic, user:, recipient: first_model_user)+ topic.topic_allowed_users.create!(user: preferred_model_user)+ topic.custom_fields[described_class::BOT_USER_PREF_ID_CUSTOM_FIELD] = preferred_model_user.id+ topic.custom_fields["ai_agent"] = general_agent.name+ topic.save_custom_fields+ post = Fabricate(:post, topic:, user:)++ expect_enqueued_with(+ job: :create_ai_reply,+ args: {+ post_id: post.id,+ agent_id: general_agent.id,+ llm_model_id: opus_model.id,+ },+ ) { described_class.schedule_reply(post) }+ end+it "rejects a private message with multiple eligible agent recipients" dosecond_agent =Fabricate(@@ -2206,6 +2249,52 @@ RSpec.describe DiscourseAi::AiBot::Playground doenddescribe ".reply_to_post" do+ it "uses the automation agent model instead of the conversation model" do+ post = Fabricate(:post, user:)+ post.topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = opus_model.id+ post.topic.save_custom_fields++ DiscourseAi::Completions::Llm.with_prepared_responses(["Automation response"]) do+ described_class.reply_to_post(+ post:,+ agent_id: general_agent.id,+ attributed_user: Discourse.system_user,+ )+ end++ reply = post.topic.posts.order(:post_number).last+ expect(reply.raw).to eq("Automation response")+ expect(reply.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ claude_2.id,+ )+ end++ it "uses the automation agent model instead of a previous reply model" do+ post = Fabricate(:post, user:)+ Fabricate(+ :post,+ topic: post.topic,+ user: bot_user,+ custom_fields: {+ DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD => opus_model.id,+ },+ )++ DiscourseAi::Completions::Llm.with_prepared_responses(["Automation response"]) do+ described_class.reply_to_post(+ post:,+ agent_id: general_agent.id,+ attributed_user: Discourse.system_user,+ )+ end++ reply = post.topic.posts.order(:post_number).last+ expect(reply.raw).to eq("Automation response")+ expect(reply.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ claude_2.id,+ )+ end+it "replies as the given user when the agent has no user" dopost = Fabricate(:post, user: user)speaker = Fabricate(:user)diff --git a/plugins/discourse-ai/spec/system/ai_bot/chat_model_label_spec.rb b/plugins/discourse-ai/spec/system/ai_bot/chat_model_label_spec.rbnew file mode 100644index 00000000000..1403a624815--- /dev/null+++ b/plugins/discourse-ai/spec/system/ai_bot/chat_model_label_spec.rb@@ -0,0 +1,41 @@+# frozen_string_literal: true++describe "AI Chat model labels" do+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:agent) { Fabricate(:ai_agent).tap(&:ensure_user!) }+ fab!(:channel) { Fabricate(:direct_message_channel, users: [user, agent.user]) }++ let(:chat) { PageObjects::Pages::Chat.new }+ let(:chat_channel) { PageObjects::Pages::ChatChannel.new }+ let(:ai_message) { PageObjects::Components::AiChatMessage.new }++ before do+ enable_current_plugin+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_0].to_s+ sign_in(user)+ chat.prefers_full_page+ end++ it "shows the model for an existing reply" do+ message = Fabricate(:chat_message, chat_channel: channel, user: agent.user)+ message.custom_fields[DiscourseAi::AiBot::CHAT_MESSAGE_AI_LLM_NAME_FIELD] = "Model 猫"+ message.save_custom_fields++ chat.visit_channel(channel)+ expect(ai_message).to have_model(message, "Model 猫")+ end++ it "shows the model when a streamed reply finishes without changing its text" do+ message = Fabricate(:chat_message, chat_channel: channel, user: agent.user, streaming: true)+ chat.visit_channel(channel)+ expect(chat_channel.messages).to have_message(id: message.id)+ expect(ai_message).to have_no_model(message)++ message.custom_fields[DiscourseAi::AiBot::CHAT_MESSAGE_AI_LLM_NAME_FIELD] = "Model 猫"+ message.save_custom_fields+ ChatSDK::Message.stop_stream(message_id: message.id, guardian: Guardian.new(agent.user))++ expect(ai_message).to have_model(message, "Model 猫")+ end+enddiff --git a/plugins/discourse-ai/spec/system/ai_bot/navigation_permissions_spec.rb b/plugins/discourse-ai/spec/system/ai_bot/navigation_permissions_spec.rbnew file mode 100644index 00000000000..0dc39727117--- /dev/null+++ b/plugins/discourse-ai/spec/system/ai_bot/navigation_permissions_spec.rb@@ -0,0 +1,47 @@+# frozen_string_literal: true++describe "AI bot navigation permissions" do+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:group)+ fab!(:llm_model)+ fab!(:agent) do+ Fabricate(+ :ai_agent,+ allowed_group_ids: [group.id],+ default_llm: llm_model,+ enabled: true,+ allow_personal_messages: true,+ ).tap(&:ensure_user!)+ end++ let(:navigation) { PageObjects::Components::AiBotNavigation.new }++ before do+ enable_current_plugin+ toggle_enabled_bots(bots: [llm_model])+ SiteSetting.ai_bot_allowed_groups = group.id.to_s+ SiteSetting.ai_bot_add_to_community_section = true+ SiteSetting.ai_bot_add_to_header = true+ SiteSetting.navigation_menu = "sidebar"+ group.add(user)+ sign_in(user)+ end++ it "hides the shortcuts when the bot is disabled" do+ SiteSetting.ai_bot_enabled = false+ visit "/latest"+ expect(navigation).to have_no_shortcuts+ end++ it "hides the shortcuts from users outside the bot allowed groups" do+ SiteSetting.ai_bot_allowed_groups = Group::AUTO_GROUPS[:staff].to_s+ visit "/latest"+ expect(navigation).to have_no_shortcuts+ end++ it "shows the shortcuts when only the agent default model is available" do+ SiteSetting.ai_bot_enabled_llms = ""+ visit "/latest"+ expect(navigation).to have_shortcuts+ end+enddiff --git a/plugins/discourse-ai/spec/system/page_objects/components/ai_bot_navigation.rb b/plugins/discourse-ai/spec/system/page_objects/components/ai_bot_navigation.rbnew file mode 100644index 00000000000..4fc1c75ee94--- /dev/null+++ b/plugins/discourse-ai/spec/system/page_objects/components/ai_bot_navigation.rb@@ -0,0 +1,17 @@+# frozen_string_literal: true++module PageObjects+ module Components+ class AiBotNavigation < Base+ def has_shortcuts?+ page.has_css?(".sidebar-section-link[data-link-name='ai-bot']") &&+ page.has_css?("header .ai-bot-button")+ end++ def has_no_shortcuts?+ page.has_no_css?(".sidebar-section-link[data-link-name='ai-bot']") &&+ page.has_no_css?("header .ai-bot-button")+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/system/page_objects/components/ai_chat_message.rb b/plugins/discourse-ai/spec/system/page_objects/components/ai_chat_message.rbnew file mode 100644index 00000000000..b33b15c626d--- /dev/null+++ b/plugins/discourse-ai/spec/system/page_objects/components/ai_chat_message.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++module PageObjects+ module Components+ class AiChatMessage < Base+ def has_model?(message, name)+ page.has_css?(+ ".chat-message-container[data-id='#{message.id}'] .ai-chat-message__model",+ text: name,+ )+ end++ def has_no_model?(message)+ page.has_no_css?(".chat-message-container[data-id='#{message.id}'] .ai-chat-message__model")+ end+ end+ end+enddiff --git a/plugins/discourse-ai/test/javascripts/components/ai-chat-model-label-test.gjs b/plugins/discourse-ai/test/javascripts/components/ai-chat-model-label-test.gjsnew file mode 100644index 00000000000..1064039da6f--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/components/ai-chat-model-label-test.gjs@@ -0,0 +1,29 @@+import { hash } from "@ember/helper";+import { render, settled } from "@ember/test-helpers";+import { module, test } from "qunit";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import ChatMessage from "discourse/plugins/chat/discourse/models/chat-message";+import AiChatModelLabel from "discourse/plugins/discourse-ai/discourse/components/ai-chat-model-label";++module("Integration | Component | AiChatModelLabel", function (hooks) {+ setupRenderingTest(hooks);++ test("shows model attribution when it arrives after the reply", async function (assert) {+ this.message = ChatMessage.create({}, { id: 1 });+ await render(+ <template>+ <AiChatModelLabel @outletArgs={{hash message=this.message}} />+ </template>+ );+ assert.dom(".ai-chat-message__model").doesNotExist();++ this.message.aiLlmName = "Model <猫>";+ await settled();+ assert.dom(".ai-chat-message__model").hasText("Model <猫>");++ this.message.aiLlmName = "Another model";+ await settled();+ assert.dom(".ai-chat-message__model").exists({ count: 1 });+ assert.dom(".ai-chat-message__model").hasText("Another model");+ });+});