A platform for community discussion. Free, open, simple.
FIX: Regenerate existing reply when retrying AI bot posts in public topics (#41715)
Rafael dos Santos Silvacb14ac05c85c
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex e74beb8e216..80aed6aee0c 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -513,6 +513,16 @@ module DiscourseAireply_user = User.find_by(id: bot.agent.class.user_id) || reply_userend+ if existing_reply_post+ if existing_reply_post.topic_id != post.topic_id+ raise Discourse::InvalidParameters.new(:reply_post_id)+ end++ if existing_reply_post.user_id != reply_user.id+ raise Discourse::InvalidParameters.new(:reply_post_id)+ end+ end+stream_reply = post.topic.private_message? if stream_reply.nil?# we need to ensure agent user is allowed to reply to the pm@@ -536,14 +546,6 @@ module DiscourseAireply_post = existing_reply_postif reply_post- if reply_post.topic_id != post.topic_id- raise Discourse::InvalidParameters.new(:reply_post_id)- end-- if reply_post.user_id != reply_user.id- raise Discourse::InvalidParameters.new(:reply_post_id)- end-reply_post.update_columns(raw: "", cooked: "")reply_post.post_custom_prompt = nilelse@@ -564,16 +566,7 @@ module DiscourseAi)end- reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD] = bot- .llm- .llm_model- .display_name- reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD] = bot- .llm- .llm_model- .id- reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD] = bot.agent.id- reply_post.save_custom_fields+ save_ai_custom_fields(reply_post)publish_update(reply_post, { raw: "" })@@ -655,6 +648,16 @@ module DiscourseAiskip_validations: true,skip_revision: true,)+ elsif existing_reply_post+ reply_post = existing_reply_post+ reply_post.post_custom_prompt = nil+ reply_post.revise(+ bot.bot_user,+ { raw: reply },+ skip_validations: true,+ force_new_version: true,+ )+ save_ai_custom_fields(reply_post)elsereply_post =PostCreator.create!(@@ -762,6 +765,19 @@ module DiscourseAiprivate+ def save_ai_custom_fields(reply_post)+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD] = bot+ .llm+ .llm_model+ .display_name+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD] = bot+ .llm+ .llm_model+ .id+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD] = bot.agent.id+ reply_post.save_custom_fields+ end+def should_stop_thinking?(partial:, context:, type:, started_thinking:, placeholder:)return false if context.skip_show_thinkingreturn false if !started_thinkingdiff --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 b96f9de9bda..c1d6915be25 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@@ -1578,4 +1578,43 @@ RSpec.describe DiscourseAi::AiBot::Playground doend}.not_to raise_errorend++ describe "retrying a reply in a public topic" do+ fab!(:public_topic, :topic)+ fab!(:prompt_post) do+ Fabricate(:post, topic: public_topic, user: user, raw: "Hello bot, can you help me?")+ end+ fab!(:bot_reply) do+ Fabricate(:post, topic: public_topic, user: bot_user, raw: "This is the first answer")+ end++ it "revises the existing reply keeping a revision instead of creating a duplicate post" do+ expect {+ DiscourseAi::Completions::Llm.with_prepared_responses(["This is the second answer"]) do+ playground.reply_to(prompt_post, existing_reply_post: bot_reply)+ end+ }.not_to change { public_topic.reload.posts.count }++ bot_reply.reload+ expect(bot_reply.raw).to eq("This is the second answer")+ expect(bot_reply.revisions.count).to eq(1)+ expect(bot_reply.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ claude_2.id,+ )+ end++ it "raises when the existing reply belongs to a different topic" do+ other_topic_reply = Fabricate(:post, user: bot_user)++ expect {+ playground.reply_to(prompt_post, existing_reply_post: other_topic_reply)+ }.to raise_error(Discourse::InvalidParameters)+ end++ it "raises when the existing reply belongs to a different user" do+ expect { playground.reply_to(prompt_post, existing_reply_post: prompt_post) }.to raise_error(+ Discourse::InvalidParameters,+ )+ end+ endend