Skip to content

A platform for community discussion. Free, open, simple.

9.9 KB
Raw Blame
# frozen_string_literal: true

describe DiscourseAi::TopicSummarization do
  fab!(:user, :admin)
  fab!(:topic) { Fabricate(:topic, highest_post_number: 2) }
  fab!(:post_1) { Fabricate(:post, topic: topic, post_number: 1) }
  fab!(:post_2) { Fabricate(:post, topic: topic, post_number: 2) }

  before do
    enable_current_plugin
    assign_fake_provider_to(:ai_default_llm_model)
    SiteSetting.ai_summarization_enabled = true
  end

  def create_cached_summary(topic, locale: SiteSetting.default_locale)
    strategy = DiscourseAi::Summarization::Strategies::TopicSummary.new(topic, locale: locale)
    content_sha = AiSummary.build_sha(strategy.targets_data.map { |target| target[:id] }.join)

    Fabricate(
      :ai_summary,
      target: topic,
      locale: locale,
      original_content_sha: content_sha,
      highest_target_number: topic.highest_post_number,
    )
  end

  let(:strategy) { DiscourseAi::Summarization.topic_summary(topic) }

  let(:summary) { "This is the final summary" }

  describe ".for" do
    it "selects the displayed locale and respects the automatic-translation preference" do
      SiteSetting.content_localization_enabled = true
      SiteSetting.content_localization_supported_locales = "he"
      topic.update!(locale: "en")
      english_summary = create_cached_summary(topic, locale: "en")
      english_summary.update!(summarized_text: "English summary")
      hebrew_summary = create_cached_summary(topic, locale: "he")
      hebrew_summary.update!(summarized_text: "סיכום בעברית")

      localized_service =
        I18n.with_locale(:he) { described_class.for(topic, user, scope: user.guardian) }
      expect(localized_service.cached_summary).to eq(hebrew_summary)

      user.user_option.update!(automatically_translate: false)
      original_service =
        I18n.with_locale(:he) { described_class.for(topic, user, scope: user.guardian) }
      expect(original_service.cached_summary).to eq(english_summary)
    end

    it "reads a cached summary without building the summarization bot" do
      cached_summary = create_cached_summary(topic)
      allow(DiscourseAi::Summarization).to receive(:build_bot).and_call_original

      expect(described_class.for(topic, nil).cached_summary).to eq(cached_summary)
      expect(DiscourseAi::Summarization).not_to have_received(:build_bot)
    end

    it "reads a cached summary without querying LLM models" do
      cached_summary = create_cached_summary(topic)
      result = nil

      queries = track_sql_queries { result = described_class.for(topic, nil).cached_summary }

      expect(result).to eq(cached_summary)
      expect(queries.grep(/FROM "llm_models"/)).to be_empty
    end

    it "can read a stored summary when generation is unavailable" do
      cached_summary = create_cached_summary(topic)
      SiteSetting.ai_summarization_agent = ""
      service = described_class.for(topic, user)

      expect(service.cached_summary).to eq(cached_summary)
      expect(service).not_to be_available
      expect(service.summarize(force_regenerate: true)).to be_nil
    end

    it "does not read or generate summaries when summarization is disabled" do
      create_cached_summary(topic)
      SiteSetting.ai_summarization_enabled = false
      service = described_class.for(topic, user)

      expect(service.cached_summary).to be_nil
      expect(service).not_to be_available
      expect(service.summarize).to be_nil
    end

    it "generates a summary through the shared topic API" do
      DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
        service = described_class.for(topic, user)

        expect(service).to be_available
        expect(service.summarize.summarized_text).to eq(summary)
      end
    end
  end

  describe "#summarize" do
    subject(:summarization) { described_class.new(strategy, user) }

    def assert_summary_is_cached(topic, summary_response)
      cached_summary =
        AiSummary.find_by(target: topic, summary_type: AiSummary.summary_types[:complete])

      expect(cached_summary.highest_target_number).to eq(topic.highest_post_number)
      expect(cached_summary.summarized_text).to eq(summary)
      expect(cached_summary.original_content_sha).to be_present
      expect(cached_summary.algorithm).to eq("fake")
    end

    context "when the content was summarized in a single chunk" do
      it "caches the summary" do
        DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
          section = summarization.summarize
          expect(section.summarized_text).to eq(summary)
          assert_summary_is_cached(topic, summary)
        end
      end

      it "returns the cached version in subsequent calls" do
        summarization.summarize

        cached_summary_text = "This is a cached summary"
        AiSummary.find_by(target: topic, summary_type: AiSummary.summary_types[:complete]).update!(
          summarized_text: cached_summary_text,
        )

        summarization = described_class.new(strategy, user)
        section = summarization.summarize
        expect(section.summarized_text).to eq(cached_summary_text)
      end
    end

    describe "invalidating cached summaries" do
      let(:cached_text) { "This is a cached summary" }

      def cached_summary
        AiSummary.find_by(target: topic, summary_type: AiSummary.summary_types[:complete])
      end

      before do
        # a bit tricky, but fold_content now caches an instance of LLM
        # once it is cached with_prepared_responses will not work as expected
        # since it is glued to the old llm instance
        # so we create the cached summary totally independently
        DiscourseAi::Completions::Llm.with_prepared_responses([cached_text]) do
          strategy = DiscourseAi::Summarization.topic_summary(topic)
          described_class.new(strategy, user).summarize
        end

        cached_summary.update!(summarized_text: cached_text, created_at: 24.hours.ago)
      end

      context "when the user can requests new summaries" do
        context "when there are no new posts" do
          it "returns the cached summary" do
            section = summarization.summarize

            expect(section.summarized_text).to eq(cached_text)
          end
        end

        context "when there are new posts" do
          before { cached_summary.update!(original_content_sha: "outdated_sha") }

          it "returns a new summary" do
            DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
              section = summarization.summarize

              expect(section.summarized_text).to eq(summary)
            end
          end

          context "when the cached summary is less than one hour old" do
            before { cached_summary.update!(created_at: 30.minutes.ago) }

            it "returns the cached summary" do
              cached_summary.update!(created_at: 30.minutes.ago)

              section = summarization.summarize

              expect(section.summarized_text).to eq(cached_text)
              expect(section.outdated).to eq(true)
            end

            it "returns a new summary if the skip_age_check flag is passed" do
              DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
                section = summarization.summarize(skip_age_check: true)

                expect(section.summarized_text).to eq(summary)
              end
            end
          end
        end

        context "when posts were edited" do
          before do
            cached_summary.update!(created_at: 30.minutes.ago)
            post_2.update_columns(last_version_at: 1.minute.from_now)
          end

          it "regenerates even when the cached summary is less than one hour old" do
            DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
              section = summarization.summarize

              expect(section.summarized_text).to eq(summary)
            end
          end
        end
      end
    end

    it "regenerates a fresh cached summary when forced" do
      cached_summary = create_cached_summary(topic)
      cached_summary.update!(summarized_text: "Cached summary")

      DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
        result = summarization.summarize(force_regenerate: true)

        expect(result).to have_attributes(id: cached_summary.id, summarized_text: summary)
      end
    end

    it "preserves a fresh cached summary when forced generation fails" do
      cached_summary = create_cached_summary(topic)
      cached_summary.update!(summarized_text: "Cached summary")

      DiscourseAi::Completions::Llm.with_prepared_responses([RuntimeError.new("LLM failed")]) do
        expect { summarization.summarize(force_regenerate: true) }.to raise_error(
          RuntimeError,
          "LLM failed",
        )
      end

      expect(cached_summary.reload.summarized_text).to eq("Cached summary")
    end

    context "when the user is anonymous" do
      let(:summarization) { described_class.new(strategy, nil) }

      it "returns a fresh cached summary" do
        cached_summary = create_cached_summary(topic)

        section = summarization.summarize

        expect(section.id).to eq(cached_summary.id)
      end

      it "returns nil when the cached summary is outdated" do
        create_cached_summary(topic)
        Fabricate(:post, topic: topic, post_number: 3)
        topic.update!(highest_post_number: 3)

        section = summarization.summarize

        expect(section).to be_nil
      end
    end

    describe "stream partial updates" do
      it "receives a blk that is passed to the underlying strategy and called with partial summaries" do
        partial_result = +""

        DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
          summarization.summarize { |partial_summary| partial_result << partial_summary }
        end

        # In a real world example, this is removed in the returned AiSummary obj.
        expect(partial_result.chomp("\"}")).to eq(summary)
      end
    end
  end
end