Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ def index
Topic
.private_messages_for_user(current_user)
.where(user: current_user) # Only show PMs where the current user is the author
.joins(:topic_users)
.where(topic_users: { user_id: bot_user_ids })
.joins(
"INNER JOIN topic_custom_fields tcf ON tcf.topic_id = topics.id
AND tcf.name = '#{DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD}'
AND tcf.value = 't'",
)
.distinct

total = base_query.count
Expand Down
6 changes: 1 addition & 5 deletions assets/javascripts/initializers/ai-conversations-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { bind } from "discourse/lib/decorators";
import { withPluginApi } from "discourse/lib/plugin-api";
import { i18n } from "discourse-i18n";
import AiBotSidebarNewConversation from "../discourse/components/ai-bot-sidebar-new-conversation";
import { isPostFromAiBot } from "../discourse/lib/ai-bot-helper";
import { AI_CONVERSATIONS_PANEL } from "../discourse/services/ai-conversations-sidebar-manager";

export default {
Expand Down Expand Up @@ -249,10 +248,7 @@ export default {
if (
topic?.archetype === "private_message" &&
topic.user_id === currentUser.id &&
(topic.is_bot_pm ||
topic.postStream.posts.some((post) =>
isPostFromAiBot(post, currentUser)
))
topic.is_bot_pm
) {
return aiConversationsSidebarManager.forceCustomSidebar();
}
Expand Down
39 changes: 39 additions & 0 deletions db/migrate/20250424163718_set_ai_bot_pm_custom_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

class SetAiBotPmCustomFields < ActiveRecord::Migration[7.2]
def up
# Set the topic custom field for past bot PMs:
# - Created by a "real" user (user_id > 0)
# - Include exactly 2 participants (creator and 1 bot)
# - One participant is a bot (ID <= -1200)

execute <<~SQL
INSERT INTO topic_custom_fields (topic_id, name, value, created_at, updated_at)
SELECT t.id, 'is_ai_bot_pm', 't', NOW(), NOW()
FROM topics t
WHERE t.archetype = 'private_message'
AND t.user_id > 0 -- Created by a real user
AND (
SELECT COUNT(*)
FROM topic_allowed_users tau
WHERE tau.topic_id = t.id
) = 2 -- Only 2 participants total
AND (
SELECT COUNT(*)
FROM topic_allowed_users tau
WHERE tau.topic_id = t.id
AND tau.user_id <= -1200 -- Bot users have IDs <= -1200
) = 1 -- One of those participants is a bot
AND NOT EXISTS (
SELECT 1
FROM topic_custom_fields tcf
WHERE tcf.topic_id = t.id
AND tcf.name = 'is_ai_bot_pm'
) -- Don't duplicate existing custom fields
SQL
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
3 changes: 3 additions & 0 deletions spec/system/ai_bot/homepage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
end

before do
pm.custom_fields[DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD] = "t"
pm.save!

SiteSetting.ai_enable_experimental_bot_ux = true
SiteSetting.ai_bot_enabled = true
Jobs.run_immediately!
Expand Down