Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit e28a4b3

Browse files
committed
Generate custom field server-side (capture all cases)
1 parent cc72e09 commit e28a4b3

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export default class AiBotConversationsHiddenSubmit extends Service {
5353
archetype: "private_message",
5454
target_recipients: this.targetUsername,
5555
meta_data: { ai_persona_id: this.personaId },
56-
topic_custom_fields: { is_ai_bot_pm: true },
5756
},
5857
});
5958

lib/ai_bot/entry_point.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,33 @@ def self.ai_share_error(topic, guardian)
6565
end
6666

6767
def inject_into(plugin)
68-
plugin.register_editable_topic_custom_field(TOPIC_AI_BOT_PM_FIELD)
68+
plugin.register_topic_custom_field_type(TOPIC_AI_BOT_PM_FIELD, :string)
69+
70+
plugin.on(:topic_created) do |topic|
71+
next if !topic.private_message?
72+
creator = topic.user
73+
74+
# Only process if creator is not a bot or system user
75+
next if DiscourseAi::AiBot::Playground.is_bot_user_id?(creator.id)
76+
# Get all bot user IDs defined by the discourse-ai plugin
77+
78+
bot_ids = DiscourseAi::AiBot::EntryPoint.all_bot_ids
79+
80+
# Check if the only recipients are bots
81+
recipients = topic.topic_allowed_users.pluck(:user_id)
82+
83+
# Remove creator from recipients for checking
84+
recipients -= [creator.id]
85+
86+
# If all remaining recipients are AI bots and there's exactly one recipient
87+
if recipients.length == 1 && (recipients - bot_ids).empty?
88+
# The only recipient is an AI bot - add the custom field to the topic
89+
topic.custom_fields[TOPIC_AI_BOT_PM_FIELD] = true
90+
91+
# Save the custom fields
92+
topic.save_custom_fields
93+
end
94+
end
6995

7096
plugin.register_modifier(:chat_allowed_bot_user_ids) do |user_ids, guardian|
7197
if guardian.user

spec/lib/modules/ai_bot/entry_point_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@
3535
expect(serializer[:current_user][:can_debug_ai_bot_conversations]).to eq(true)
3636
end
3737

38+
describe "adding TOPIC_AI_BOT_PM_FIELD to topic custom fields" do
39+
it "is added when user PMs a single bot" do
40+
topic = PostCreator.create!(admin, post_args).topic
41+
expect(topic.reload.custom_fields[DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD]).to eq("t")
42+
end
43+
44+
it "is not added when user PMs a bot and another user" do
45+
user = Fabricate(:user)
46+
post_args[:target_usernames] = [gpt_bot.username, user.username].join(",")
47+
topic = PostCreator.create!(admin, post_args).topic
48+
expect(topic.reload.custom_fields[DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD]).to be_nil
49+
end
50+
end
51+
3852
it "adds information about forcing default llm to current_user_serializer" do
3953
Group.refresh_automatic_groups!
4054

0 commit comments

Comments
 (0)