A platform for community discussion. Free, open, simple.
FEATURE: Auto enable AI search (full page) when no regular results (#35336)
Keegan George7d4f756a4f88
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-full-page-search.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-full-page-search.gjsindex 9861028b5d4..2b18add129e 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-full-page-search.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-full-page-search.gjs@@ -25,17 +25,29 @@ export default class AiFullPageSearch extends Component {@tracked AiResults = [];@tracked showingAiResults = false;@tracked sortOrder = this.args.sortOrder;+ @tracked autoEnabledForZeroResults = false;+ @tracked shouldAutoEnableWhenAiReady = false;initialSearchTerm = this.args.searchTerm;constructor() {super(...arguments);this.appEvents.on("full-page-search:trigger-search", this, this.onSearch);+ this.appEvents.on(+ "search:search_result_view",+ this,+ this.onSearchResultsLoaded+ );this.onSearch();}willDestroy() {super.willDestroy(...arguments);this.appEvents.off("full-page-search:trigger-search", this, this.onSearch);+ this.appEvents.off(+ "search:search_result_view",+ this,+ this.onSearchResultsLoaded+ );}@action@@ -46,10 +58,39 @@ export default class AiFullPageSearch extends Component {this.initialSearchTerm = this.args.searchTerm;this.searching = true;+ this.autoEnabledForZeroResults = false;+ this.shouldAutoEnableWhenAiReady = false;this.resetAiResults();return this.performHyDESearch();}+ @action+ onSearchResultsLoaded() {+ // enable AI results if we have zero regular results and AI results are ready+ if (+ this.hasZeroRegularResults &&+ !this.showingAiResults &&+ !this.autoEnabledForZeroResults &&+ this.AiResults.length > 0+ ) {+ this.autoEnabledForZeroResults = true;+ this.showingAiResults = true;+ this.args.addSearchResults(this.AiResults, "topic_id");+ this.appEvents.trigger(AI_RESULTS_TOGGLED, {+ enabled: true,+ });+ }+ // AI results not ready yet, auto-enable when ready+ else if (+ this.hasZeroRegularResults &&+ !this.showingAiResults &&+ !this.autoEnabledForZeroResults &&+ this.AiResults.length === 0+ ) {+ this.shouldAutoEnableWhenAiReady = true;+ }+ }+get disableToggleSwitch() {if (this.searching ||@@ -64,6 +105,17 @@ export default class AiFullPageSearch extends Component {return this.sortOrder === 0;}+ get hasZeroRegularResults() {+ if (!this.args.model) {+ return false;+ }+ const postsCount = this.args.model.posts?.length || 0;+ const categoriesCount = this.args.model.categories?.length || 0;+ const tagsCount = this.args.model.tags?.length || 0;+ const usersCount = this.args.model.users?.length || 0;+ return postsCount + categoriesCount + tagsCount + usersCount === 0;+ }+get searchStateText() {if (!this.validSearchOrder) {return i18n(@@ -71,6 +123,20 @@ export default class AiFullPageSearch extends Component {);}+ // Auto-enabled and showing results:+ if (+ this.autoEnabledForZeroResults &&+ this.showingAiResults &&+ this.AiResults.length > 0+ ) {+ return i18n(+ "discourse_ai.embeddings.semantic_search_results.zero_results_expanded",+ {+ count: this.AiResults.length,+ }+ );+ }+// Search loading:if (this.searching) {return i18n("discourse_ai.embeddings.semantic_search_loading");@@ -160,6 +226,7 @@ export default class AiFullPageSearch extends Component {});if (this.showingAiResults) {this.args.addSearchResults([], "topic_id");+ this.autoEnabledForZeroResults = false;} else {this.args.addSearchResults(this.AiResults, "topic_id");}@@ -191,6 +258,21 @@ export default class AiFullPageSearch extends Component {});this.AiResults = model.posts;++ if (+ this.shouldAutoEnableWhenAiReady &&+ !this.showingAiResults &&+ !this.autoEnabledForZeroResults &&+ this.AiResults.length > 0+ ) {+ this.autoEnabledForZeroResults = true;+ this.showingAiResults = true;+ this.shouldAutoEnableWhenAiReady = false;+ this.args.addSearchResults(this.AiResults, "topic_id");+ this.appEvents.trigger(AI_RESULTS_TOGGLED, {+ enabled: true,+ });+ }}).catch(popupAjaxError).finally(() => {diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-below-search-header/ai-full-page-search.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-below-search-header/ai-full-page-search.gjsindex a0f1c5d8075..96112eb7b42 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-below-search-header/ai-full-page-search.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-below-search-header/ai-full-page-search.gjs@@ -12,6 +12,7 @@ export default class AiFullPageSearchConnector extends Component {@searchTerm={{@outletArgs.search}}@searchType={{@outletArgs.type}}@addSearchResults={{@outletArgs.addSearchResults}}+ @model={{@outletArgs.model}}/></template>}diff --git a/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scss b/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scssindex e36772915b0..c4e5069eae2 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/embeddings/common/semantic-search.scss@@ -36,7 +36,8 @@}}- svg {+ .d-icon-discourse-sparkles,+ .d-icon-far-circle-question {font-size: var(--font-down-1);color: var(--primary-high);}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex c5f703a3915..6c683ebe576 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -869,6 +869,7 @@ en:none: "Sorry, our AI search found no matching topics"new: "Press 'search' to begin looking for new results with AI"unavailable: "AI results unavailable"+ zero_results_expanded: "Zero results found, showing %{count} AI results"semantic_search_tooltips:results_explanation: "When enabled, additional AI search results will be added below."invalid_sort: "Search results must be sorted by Relevance to display AI results"