diff --git a/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs b/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs index bbf10160..dbc5ab86 100644 --- a/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs +++ b/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs @@ -112,7 +112,9 @@ export default class AiTagSuggester extends Component { } #tagSelectorHasValues() { - return this.args.composer?.tags && this.args.composer?.tags.length > 0; + const model = this.args.composer ? this.args.composer : this.args.buffered; + + return model.get("tags") && model.get("tags").length > 0; } #removedAppliedTag(suggestion) { diff --git a/lib/ai_helper/semantic_categorizer.rb b/lib/ai_helper/semantic_categorizer.rb index 612030c8..72759a6c 100644 --- a/lib/ai_helper/semantic_categorizer.rb +++ b/lib/ai_helper/semantic_categorizer.rb @@ -78,7 +78,7 @@ module DiscourseAi .group_by { |c| c[:name] } .map { |name, scores| { name: name, score: scores.sum { |s| s[:score] } } } .sort_by { |c| -c[:score] } - .take(5) + .take(7) .then do |tags| models = Tag.where(name: tags.map { _1[:name] }).index_by(&:name) tags.map do |tag| diff --git a/spec/system/ai_helper/ai_composer_helper_spec.rb b/spec/system/ai_helper/ai_composer_helper_spec.rb index 662fd9d2..324d8c4b 100644 --- a/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -17,6 +17,7 @@ RSpec.describe "AI Composer helper", type: :system, js: true do let(:diff_modal) { PageObjects::Modals::DiffModal.new } let(:ai_suggestion_dropdown) { PageObjects::Components::AiSuggestionDropdown.new } let(:toasts) { PageObjects::Components::Toasts.new } + let(:topic_page) { PageObjects::Pages::Topic.new } fab!(:category) fab!(:category_2) { Fabricate(:category) } @@ -25,6 +26,18 @@ RSpec.describe "AI Composer helper", type: :system, js: true do fab!(:cloud) { Fabricate(:tag) } fab!(:feedback) { Fabricate(:tag) } fab!(:review) { Fabricate(:tag) } + fab!(:topic) { Fabricate(:topic, category: category, tags: [video, music]) } + fab!(:post) do + Fabricate( + :post, + topic: topic, + raw: + "I like to eat pie. It is a very good dessert. Some people are wasteful by throwing pie at others but I do not do that. I always eat the pie.", + ) + end + fab!(:post_2) do + Fabricate(:post, topic: topic, raw: "La lluvia en España se queda principalmente en el avión.") + end def trigger_composer_helper(content) visit("/latest") @@ -266,7 +279,7 @@ RSpec.describe "AI Composer helper", type: :system, js: true do it "updates the tag with the suggested tag" do response = Tag - .take(5) + .take(7) .pluck(:name) .map { |s| { name: s, score: rand(0.0...45.0) } } .sort { |h| h[:score] } @@ -286,6 +299,25 @@ RSpec.describe "AI Composer helper", type: :system, js: true do expect(tag_selector["data-name"]).to eq(suggestion) end + + it "does not suggest tags that already exist" do + response = + Tag + .take(7) + .pluck(:name) + .map { |s| { name: s, score: rand(0.0...45.0) } } + .sort { |h| h[:score] } + DiscourseAi::AiHelper::SemanticCategorizer.any_instance.stubs(:tags).returns(response) + + topic_page.visit_topic(topic) + page.find(".edit-topic").click + page.find(".ai-tag-suggester-trigger").click + tag1_css = ".ai-tag-suggester-content btn[data-name='#{video.name}']" + tag2_css = ".ai-tag-suggester-content btn[data-name='#{music.name}']" + + expect(page).to have_no_css(tag1_css) + expect(page).to have_no_css(tag2_css) + end end context "when AI helper is disabled" do