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

Commit 53c086a

Browse files
committed
FIX: do not add bot user to PM when using responders
1 parent 1ce25c5 commit 53c086a

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/ai_bot/playground.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def self.schedule_reply(post)
162162
end
163163
end
164164

165-
def self.reply_to_post(post:, user: nil, persona_id: nil, whisper: nil)
165+
def self.reply_to_post(post:, user: nil, persona_id: nil, whisper: nil, add_user_to_pm: false)
166166
ai_persona = AiPersona.find_by(id: persona_id)
167167
raise Discourse::InvalidParameters.new(:persona_id) if !ai_persona
168168
persona_class = ai_persona.class_instance
@@ -173,7 +173,12 @@ def self.reply_to_post(post:, user: nil, persona_id: nil, whisper: nil)
173173
bot = DiscourseAi::AiBot::Bot.as(bot_user, persona: persona)
174174
playground = DiscourseAi::AiBot::Playground.new(bot)
175175

176-
playground.reply_to(post, whisper: whisper, context_style: :topic)
176+
playground.reply_to(
177+
post,
178+
whisper: whisper,
179+
context_style: :topic,
180+
add_user_to_pm: add_user_to_pm,
181+
)
177182
end
178183

179184
def initialize(bot)
@@ -433,7 +438,14 @@ def get_context(participants:, conversation_context:, user:, skip_tool_details:
433438
result
434439
end
435440

436-
def reply_to(post, custom_instructions: nil, whisper: nil, context_style: nil, &blk)
441+
def reply_to(
442+
post,
443+
custom_instructions: nil,
444+
whisper: nil,
445+
context_style: nil,
446+
add_user_to_pm: true,
447+
&blk
448+
)
437449
# this is a multithreading issue
438450
# post custom prompt is needed and it may not
439451
# be properly loaded, ensure it is loaded
@@ -470,7 +482,7 @@ def reply_to(post, custom_instructions: nil, whisper: nil, context_style: nil, &
470482
stream_reply = post.topic.private_message?
471483

472484
# we need to ensure persona user is allowed to reply to the pm
473-
if post.topic.private_message?
485+
if post.topic.private_message? && add_user_to_pm
474486
if !post.topic.topic_allowed_users.exists?(user_id: reply_user.id)
475487
post.topic.topic_allowed_users.create!(user_id: reply_user.id)
476488
end
@@ -485,6 +497,7 @@ def reply_to(post, custom_instructions: nil, whisper: nil, context_style: nil, &
485497
skip_validations: true,
486498
skip_jobs: true,
487499
post_type: post_type,
500+
skip_guardian: true,
488501
)
489502

490503
publish_update(reply_post, { raw: reply_post.cooked })
@@ -560,6 +573,7 @@ def reply_to(post, custom_instructions: nil, whisper: nil, context_style: nil, &
560573
raw: reply,
561574
skip_validations: true,
562575
post_type: post_type,
576+
skip_guardian: true,
563577
)
564578
end
565579

spec/lib/discourse_automation/llm_persona_triage_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def add_automation_field(name, value, type: "text")
165165
expect(context).to include("support")
166166
end
167167

168-
it "passes private message metadata in context when responding to PM" do
168+
it "interacts correctly with PMs" do
169169
# Create a private message topic
170170
pm_topic = Fabricate(:private_message_topic, user: user, title: "Important PM")
171171

@@ -190,6 +190,8 @@ def add_automation_field(name, value, type: "text")
190190
# Capture the prompt sent to the LLM
191191
prompt = nil
192192

193+
original_user_ids = pm_topic.topic_allowed_users.pluck(:user_id)
194+
193195
DiscourseAi::Completions::Llm.with_prepared_responses(
194196
["I've received your private message"],
195197
) do |_, _, _prompts|
@@ -204,5 +206,13 @@ def add_automation_field(name, value, type: "text")
204206
expect(context).to include("Important PM")
205207
expect(context).to include(pm_post.raw)
206208
expect(context).to include(pm_post2.raw)
209+
210+
reply = pm_topic.posts.order(:post_number).last
211+
expect(reply.raw).to eq("I've received your private message")
212+
213+
topic = reply.topic
214+
215+
# should not inject persona into allowed users
216+
expect(topic.topic_allowed_users.pluck(:user_id).sort).to eq(original_user_ids.sort)
207217
end
208218
end

0 commit comments

Comments
 (0)