A platform for community discussion. Free, open, simple.
FIX: Fall back when conversation models are deleted (#45136)
Samc92bd044aa45
diff --git a/plugins/discourse-ai/lib/ai_bot/conversation_route.rb b/plugins/discourse-ai/lib/ai_bot/conversation_route.rbindex 08118dad7a8..13d6478c5b4 100644--- a/plugins/discourse-ai/lib/ai_bot/conversation_route.rb+++ b/plugins/discourse-ai/lib/ai_bot/conversation_route.rb@@ -21,6 +21,7 @@ module DiscourseAi:model_source,:authorization_user,:modality,+ :replaced_topic_model,) dodef agent_idagent_record.id@@ -90,7 +91,7 @@ module DiscourseAiendvalidate_recipient!(agent_record)- model, model_source = resolve_model(agent_record, agent_class)+ model, model_source, replaced_topic_model = resolve_model(agent_record, agent_class)Result.new(agent_record:,@@ -100,6 +101,7 @@ module DiscourseAimodel_source:,authorization_user: @authorization_user,modality: @modality,+ replaced_topic_model: !!replaced_topic_model,)end@@ -202,11 +204,14 @@ module DiscourseAipersisted_topic_id = topic_model_idtopic_id = persisted_topic_id if requested_id.blank?legacy_id = legacy_model_id if requested_id.blank? && topic_id.blank?+ echoed_topic_model =+ @selection_source == :request && requested_id.present? &&+ requested_id == persisted_topic_idif agent_class.force_default_llmforced_id = agent_record.default_llm_idraise_error(:ai_llm_model_id, "no_model_available") if forced_id.blank?- if requested_id.present? && requested_id != forced_id+ if requested_id.present? && requested_id != forced_id && !echoed_topic_modelraise_error(:ai_llm_model_id, "model_conflicts_with_agent")endreturn find_model!(forced_id), :forced@@ -215,17 +220,28 @@ module DiscourseAiselected_id = requested_id || topic_id || legacy_idif selected_idsource = model_selection_source(requested_id, topic_id)+ model = LlmModel.find_by(id: selected_id)+ inherited_topic_model = topic_id.present? || echoed_topic_model+ if model.blank? && inherited_topic_model+ model, source = resolve_default_model(agent_record)+ return model, source, true+ end+configured_source = configured_model_source(selected_id, agent_record)- inherited_configured_model =- source == :topic || (source == :request && persisted_topic_id == selected_id)+ inherited_configured_model = source == :topic || echoed_topic_modelsource = configured_source if configured_source && inherited_configured_modelif %i[request topic].include?(source) &&!LlmModel.enabled_chat_bot_ids.include?(selected_id)raise_error(:ai_llm_model_id, "model_not_selectable")end- return find_model!(selected_id), source+ raise_error(:ai_llm_model_id, "model_unavailable") if model.blank?+ return model, sourceend+ resolve_default_model(agent_record)+ end++ def resolve_default_model(agent_record)default_id =agent_record.default_llm_id.presence || SiteSetting.ai_default_llm_model.presenceraise_error(:ai_llm_model_id, "no_model_available") if default_id.blank?diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex 318595d5a5f..28644f32e36 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -204,7 +204,7 @@ module DiscourseAipersist_agent: requested_agent_id.present? || topic_agent_id.blank?,persist_model:requested_model_id.present? || requested_agent_id.present? ||- (topic_model_id.blank? && !turn_only_agent_mention),+ ((topic_model_id.blank? || route.replaced_topic_model) && !turn_only_agent_mention),)add_agent_to_private_message!(post.topic, route.speaker) if private_messagediff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/conversation_route_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/conversation_route_spec.rbindex 0b61c12008c..ed8fd14a44a 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/conversation_route_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/conversation_route_spec.rb@@ -63,6 +63,98 @@ RSpec.describe DiscourseAi::AiBot::ConversationRoute do)end+ it "uses the current agent default after the conversation model is deleted" do+ topic = Fabricate(:private_message_topic, user:, recipient: agent.user)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = first_model.id+ topic.save_custom_fields+ agent.update!(default_llm: second_model)+ first_model.destroy!+ SiteSetting.ai_bot_enabled_llms = ""++ route = described_class.resolve(authorization_user: user, modality: :personal_message, topic:)++ expect(route.model).to eq(second_model)+ expect(route.model_source).to eq(:agent_default)+ end++ it "uses the current agent default when the client echoes a deleted conversation model" do+ topic = Fabricate(:private_message_topic, user:, recipient: agent.user)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = first_model.id+ topic.save_custom_fields+ agent.update!(default_llm: second_model)+ first_model.destroy!++ route =+ described_class.resolve(+ authorization_user: user,+ modality: :personal_message,+ topic:,+ llm_model_id: first_model.id,+ )++ expect(route.model).to eq(second_model)+ expect(route.model_source).to eq(:agent_default)+ end++ it "uses the current site default when the deleted conversation model has no agent replacement" do+ topic = Fabricate(:private_message_topic, user:, recipient: agent.user)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = first_model.id+ topic.save_custom_fields+ agent.update!(default_llm: nil)+ site_default = assign_fake_provider_to(:ai_default_llm_model)+ first_model.destroy!++ route = described_class.resolve(authorization_user: user, modality: :personal_message, topic:)++ expect(route.model).to eq(site_default)+ expect(route.model_source).to eq(:site_default)+ end++ it "rejects an explicit deleted model rather than silently using the agent default" do+ topic = Fabricate(:private_message_topic, user:, recipient: agent.user)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = first_model.id+ topic.save_custom_fields+ second_model.destroy!++ expect do+ described_class.resolve(+ authorization_user: user,+ modality: :personal_message,+ topic:,+ llm_model_id: second_model.id,+ )+ end.to raise_error(+ described_class::Error,+ I18n.t("discourse_ai.ai_bot.errors.model_unavailable"),+ )+ end++ it "preserves a deleted snapshot model failure even when it matches the conversation model" do+ topic = Fabricate(:private_message_topic, user:, recipient: agent.user)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = first_model.id+ topic.save_custom_fields+ agent.update!(default_llm: second_model)+ first_model.destroy!++ expect do+ described_class.resolve(+ authorization_user: user,+ modality: :personal_message,+ topic:,+ llm_model_id: first_model.id,+ selection_source: :snapshot,+ )+ end.to raise_error(+ described_class::Error,+ I18n.t("discourse_ai.ai_bot.errors.model_unavailable"),+ )+ end+it "ignores a zero model provenance value during legacy resolution" dotopic = Fabricate(:private_message_topic, user: user, recipient: agent.user)topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id@@ -99,6 +191,39 @@ RSpec.describe DiscourseAi::AiBot::ConversationRoute do)end+ it "uses the current forced model when the client echoes a deleted conversation model" do+ topic = Fabricate(:private_message_topic, user:, recipient: agent.user)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = first_model.id+ topic.save_custom_fields+ agent.update!(default_llm: second_model, force_default_llm: true)+ first_model.destroy!++ route =+ described_class.resolve(+ authorization_user: user,+ modality: :personal_message,+ topic:,+ llm_model_id: first_model.id,+ )++ expect(route.model).to eq(second_model)+ expect(route.model_source).to eq(:forced)++ expect do+ described_class.resolve(+ authorization_user: user,+ modality: :personal_message,+ topic:,+ llm_model_id: first_model.id,+ selection_source: :snapshot,+ )+ end.to raise_error(+ described_class::Error,+ I18n.t("discourse_ai.ai_bot.errors.model_conflicts_with_agent"),+ )+ end+it "allows a configured default model outside the public picker" doSiteSetting.ai_bot_enabled_llms = second_model.id.to_sdiff --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 feebb2b6a4c..284f238d8f5 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@@ -1340,6 +1340,30 @@ RSpec.describe DiscourseAi::AiBot::Playground do)end+ it "preserves the conversation model for a per-turn legacy model mention" do+ legacy_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)+ claude_2.update_column(:user_id, legacy_user.id)+ agent.update!(allow_topic_mentions: true)+ topic = Fabricate(:topic, user: admin)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = opus_model.id+ topic.save_custom_fields++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["One-turn legacy response"],+ llm: claude_2,+ ) { create_post(topic:, user: admin, raw: "@#{legacy_user.username} answer this turn") }++ response = topic.reload.posts.order(:post_number).last+ expect(response.raw).to eq("One-turn legacy response")+ expect(response.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ claude_2.id,+ )+ expect(topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ opus_model.id,+ )+ end+it "normalizes a reply to a public legacy model post to the topic agent" dolegacy_user = Fabricate(:user, id: DiscourseAi::BotUser.next_id)claude_2.update_column(:user_id, legacy_user.id)@@ -1502,6 +1526,37 @@ RSpec.describe DiscourseAi::AiBot::Playground do)end+ it "preserves a deleted conversation model when another agent is mentioned for one turn" do+ mentioned_agent =+ Fabricate(+ :ai_agent,+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ allow_topic_mentions: true,+ default_llm: opus_model,+ ).tap(&:ensure_user!)+ topic = Fabricate(:topic, user: admin)+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD] = claude_2.id+ topic.save_custom_fields+ agent.update!(default_llm: opus_model)+ claude_2.destroy!++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["One-turn replacement response"],+ llm: opus_model,+ ) do+ create_post(topic:, user: admin, raw: "@#{mentioned_agent.user.username} answer this turn")+ end++ response = topic.reload.posts.order(:post_number).last+ expect(response.raw).to eq("One-turn replacement response")+ expect(response.user).to eq(mentioned_agent.user)+ expect(topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD].to_i).to eq(agent.id)+ expect(topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ claude_2.id,+ )+ end+it "persists a missing model default when continuing an existing agent conversation" dotopic = Fabricate(:private_message_topic, user: admin, recipient: agent.user)topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD] = agent.id@@ -1516,6 +1571,48 @@ RSpec.describe DiscourseAi::AiBot::Playground do).to eq(claude_2.id)end+ it "continues an existing conversation with the current agent model after deleting the old model" do+ post = nil+ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["ORCHID-726", "Code word"],+ llm: claude_2,+ ) do+ post =+ create_post(+ title: "Remember a code word",+ raw: "Remember ORCHID-726",+ target_usernames: agent.user.username,+ archetype: Archetype.private_message,+ user: admin,+ custom_fields: {+ DiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD => agent.id,+ DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD => claude_2.id,+ },+ )+ end++ expect(post.topic.posts.order(:post_number).last.raw).to eq("ORCHID-726")++ agent.update!(default_llm: opus_model)+ claude_2.destroy!+ toggle_enabled_bots(bots: [])++ DiscourseAi::Completions::Llm.with_prepared_responses(["ORCHID-726"], llm: opus_model) do+ create_post(topic: post.topic, user: admin, raw: "What was the code word?")+ end++ topic = post.topic.reload+ response = topic.posts.order(:post_number).last+ expect(response.raw).to eq("ORCHID-726")+ expect(response.user).to eq(agent.user)+ expect(response.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ opus_model.id,+ )+ expect(topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ opus_model.id,+ )+ end+it "accepts a configured topic default sent back unchanged by the client" doselectable_model = Fabricate(:llm_model)toggle_enabled_bots(bots: [selectable_model])