Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/models/ai_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ def outdated
#
# Indexes
#
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
# idx_on_target_id_target_type_summary_type_3355609fbb (target_id,target_type,summary_type) UNIQUE
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
#
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ discourse_ai:
default: 0
min: 0
max: 1000
hidden: true
ai_hot_topic_gists_allowed_groups:
type: group_list
list_type: compact
Expand Down
19 changes: 19 additions & 0 deletions db/migrate/20241125132452_unique_ai_summaries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true
class UniqueAiSummaries < ActiveRecord::Migration[7.1]
def up
execute <<~SQL
DELETE FROM ai_summaries ais1
USING ai_summaries ais2
WHERE ais1.id < ais2.id
AND ais1.target_id = ais2.target_id
AND ais1.target_type = ais2.target_type
AND ais1.summary_type = ais2.summary_type
SQL

add_index :ai_summaries, %i[target_id target_type summary_type], unique: true
end

def down
remove_index :ai_summaries, column: %i[target_id target_type summary_type]
end
end
8 changes: 2 additions & 6 deletions lib/summarization/entry_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def inject_into(plugin)
plugin.register_modifier(:topic_query_create_list_topics) do |topics, options|
if Discourse.filters.include?(options[:filter]) && SiteSetting.ai_summarization_enabled &&
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0
topics.includes(:ai_summaries)
topics.includes(:ai_gist_summary)
else
topics
end
Expand All @@ -32,11 +32,7 @@ def inject_into(plugin)
include_condition: -> { scope.can_see_gists? },
) do
return if !Discourse.filters.include?(options[:filter])
summaries = object.ai_summaries.to_a

# Summaries should always have one or zero elements here.
# This is an extra safeguard to avoid including regular summaries.
summaries.find { |s| s.summary_type == "gist" }&.summarized_text
object.ai_gist_summary&.summarized_text
end

# To make sure hot topic gists are inmediately up to date, we rely on this event
Expand Down
9 changes: 8 additions & 1 deletion lib/topic_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ module DiscourseAi
module TopicExtensions
extend ActiveSupport::Concern

prepended { has_many :ai_summaries, as: :target }
prepended do
has_many :ai_summaries, as: :target

has_one :ai_gist_summary,
-> { where(summary_type: AiSummary.summary_types[:gist]) },
class_name: "AiSummary",
as: :target
end
end
end