From f3b3312ce04f53283dad5ca0ce967f204ac38f09 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Fri, 22 Mar 2024 12:34:18 +0800 Subject: [PATCH] FIX: Avoid replying to the reply user for llm_triage automation (#544) Avoid replying to the reply user. This causes an infinite conversation from the reply_user to the reply_user --- discourse_automation/llm_triage.rb | 16 +++++----- .../discourse_automation/llm_triage_spec.rb | 29 ++++++++++++++----- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/discourse_automation/llm_triage.rb b/discourse_automation/llm_triage.rb index 2ba73c08..6b54f502 100644 --- a/discourse_automation/llm_triage.rb +++ b/discourse_automation/llm_triage.rb @@ -34,8 +34,15 @@ if defined?(DiscourseAutomation) field :canned_reply, component: :message field :canned_reply_user, component: :user - script do |context, fields, automation| + script do |context, fields| post = context["post"] + canned_reply = fields.dig("canned_reply", "value") + canned_reply_user = fields.dig("canned_reply_user", "value") + + # nothing to do if we already replied + next if post.user.username == canned_reply_user + next if post.raw.strip == canned_reply.to_s.strip + system_prompt = fields["system_prompt"]["value"] search_for_text = fields["search_for_text"]["value"] model = fields["model"]["value"] @@ -49,13 +56,6 @@ if defined?(DiscourseAutomation) tags = fields.dig("tags", "value") hide_topic = fields.dig("hide_topic", "value") flag_post = fields.dig("flag_post", "value") - canned_reply = fields.dig("canned_reply", "value") - canned_reply_user = fields.dig("canned_reply_user", "value") - - if post.raw.strip == canned_reply.to_s.strip - # nothing to do if we already replied - next - end begin DiscourseAi::Automation::LlmTriage.handle( diff --git a/spec/lib/discourse_automation/llm_triage_spec.rb b/spec/lib/discourse_automation/llm_triage_spec.rb index ddb46e16..7651b69c 100644 --- a/spec/lib/discourse_automation/llm_triage_spec.rb +++ b/spec/lib/discourse_automation/llm_triage_spec.rb @@ -3,7 +3,8 @@ return if !defined?(DiscourseAutomation) describe DiscourseAi::Automation::LlmTriage do - fab!(:post) + fab!(:category) + fab!(:reply_user) { Fabricate(:user) } let(:automation) { Fabricate(:automation, script: "llm_triage", enabled: true) } @@ -18,12 +19,8 @@ describe DiscourseAi::Automation::LlmTriage do ) end - it "can trigger via automation" do + before do SiteSetting.tagging_enabled = true - - category = Fabricate(:category) - user = Fabricate(:user) - add_automation_field("system_prompt", "hello %%POST%%") add_automation_field("search_for_text", "bad") add_automation_field("model", "gpt-4") @@ -32,7 +29,11 @@ describe DiscourseAi::Automation::LlmTriage do add_automation_field("hide_topic", true, type: "boolean") add_automation_field("flag_post", true, type: "boolean") add_automation_field("canned_reply", "Yo this is a reply") - add_automation_field("canned_reply_user", user.username, type: "user") + add_automation_field("canned_reply_user", reply_user.username, type: "user") + end + + it "can trigger via automation" do + post = Fabricate(:post) DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do automation.running_in_background! @@ -45,6 +46,18 @@ describe DiscourseAi::Automation::LlmTriage do expect(topic.visible).to eq(false) reply = topic.posts.order(:post_number).last expect(reply.raw).to eq("Yo this is a reply") - expect(reply.user.id).to eq(user.id) + expect(reply.user.id).to eq(reply_user.id) + end + + it "does not reply to the canned_reply_user" do + post = Fabricate(:post, user: reply_user) + + DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do + automation.running_in_background! + automation.trigger!({ "post" => post }) + end + + last_post = post.topic.reload.posts.order(:post_number).last + expect(last_post.raw).to eq post.raw end end