Skip to content

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

UX: show message explaining AI added search results (#38743)

Kris96a419887f19
diff --git a/frontend/discourse/app/templates/full-page-search.gjs b/frontend/discourse/app/templates/full-page-search.gjs
index 8a59fd7f6cd..dda9898a9f9 100644
--- a/frontend/discourse/app/templates/full-page-search.gjs
+++ b/frontend/discourse/app/templates/full-page-search.gjs
@@ -226,6 +226,14 @@ export default <template>
@controller.usingDefaultSearchType @controller.customSearchType
)
}}
+ <PluginOutlet
+ @name="full-page-search-before-results"
+ @connectorTagName="div"
+ @outletArgs={{lazyHash
+ }}
+ />
<SearchResultEntries
@posts={{@controller.searchResultPosts}}
@bulkSelectEnabled={{@controller.bulkSelectEnabled}}
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.gjs
index 3af559b2cd2..114ac59e6ba 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
@@ -8,6 +8,7 @@ import { service } from "@ember/service";
import DToggleSwitch from "discourse/components/d-toggle-switch";
import { SEARCH_TYPE_DEFAULT } from "discourse/controllers/full-page-search";
import DTooltip from "discourse/float-kit/components/d-tooltip";
+import bodyClass from "discourse/helpers/body-class";
import concatClass from "discourse/helpers/concat-class";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
@@ -77,6 +78,7 @@ export default class AiFullPageSearch extends Component {
this.args.addSearchResults(this.AiResults, "topic_id");
this.appEvents.trigger(AI_RESULTS_TOGGLED, {
enabled: true,
+ autoEnabled: true,
});
}
// AI results not ready yet, auto-enable when ready
@@ -201,7 +203,9 @@ export default class AiFullPageSearch extends Component {
toggleAiResults() {
this.appEvents.trigger(AI_RESULTS_TOGGLED, {
enabled: !this.showingAiResults,
+ autoEnabled: this.hasZeroRegularResults,
});
+
if (this.showingAiResults) {
this.args.addSearchResults([], "topic_id");
this.autoEnabledForZeroResults = false;
@@ -249,6 +253,7 @@ export default class AiFullPageSearch extends Component {
this.args.addSearchResults(this.AiResults, "topic_id");
this.appEvents.trigger(AI_RESULTS_TOGGLED, {
enabled: true,
+ autoEnabled: true,
});
}
})
@@ -275,6 +280,7 @@ export default class AiFullPageSearch extends Component {
}
<template>
+ {{bodyClass (if this.searching "ai-semantic-search-loading")}}
<div
{{didUpdate this.sortChanged @sortOrder}}
class="semantic-search__container search-results"
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-before-results/ai-search-results-notice.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-before-results/ai-search-results-notice.gjs
new file mode 100644
index 00000000000..eb25abf37a9
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/full-page-search-before-results/ai-search-results-notice.gjs
@@ -0,0 +1,44 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { service } from "@ember/service";
+import { modifier } from "ember-modifier";
+import { i18n } from "discourse-i18n";
+
+const AI_RESULTS_TOGGLED = "full-page-search:ai-results-toggled";
+
+export default class AiSearchResultsNotice extends Component {
+ static shouldRender(args, { siteSettings }) {
+ return siteSettings.ai_embeddings_semantic_search_enabled;
+ }
+
+ @service appEvents;
+
+ @tracked showNotice = false;
+
+ listenForAiResults = modifier(() => {
+ const onToggled = ({ enabled, autoEnabled }) => {
+ this.showNotice = enabled && autoEnabled;
+ };
+ const onNewSearch = () => {
+ this.showNotice = false;
+ };
+
+ this.appEvents.on(AI_RESULTS_TOGGLED, onToggled);
+ this.appEvents.on("full-page-search:trigger-search", onNewSearch);
+
+ return () => {
+ this.appEvents.off(AI_RESULTS_TOGGLED, onToggled);
+ this.appEvents.off("full-page-search:trigger-search", onNewSearch);
+ };
+ });
+
+ <template>
+ <span {{this.listenForAiResults}}>
+ {{#if this.showNotice}}
+ <div class="ai-search-results-notice">
+ {{i18n "discourse_ai.embeddings.ai_results_notice.no_results"}}
+ </div>
+ {{/if}}
+ </span>
+ </template>
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs
index fb19b98ad65..f736ccc92fe 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs
@@ -1,11 +1,11 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import { service } from "@ember/service";
import { modifier } from "ember-modifier";
import { MODIFIER_REGEXP } from "discourse/components/search-menu";
-import loadingSpinner from "discourse/helpers/loading-spinner";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { isValidSearchTerm, translateResults } from "discourse/lib/search";
@@ -17,6 +17,7 @@ export default class AiQuickSearch extends Component {
return siteSettings.ai_embeddings_semantic_quick_search_enabled;
}
+ @service appEvents;
@service search;
@service siteSettings;
@@ -29,26 +30,18 @@ export default class AiQuickSearch extends Component {
return;
}
- const resultsContainer = document.querySelector(".search-menu .results");
- if (resultsContainer) {
- resultsContainer.classList.add("has-ai-search-results");
- }
-
const resultTypes = this.search.results?.resultTypes || [];
resultTypes.forEach((resultType) => {
resultType.results?.forEach((result) => {
if (result.aiGenerated) {
const topicId = result.topic?.id || result.topic_id;
if (topicId) {
- const listItem = document
+ document
.querySelector(
`.search-menu .list .item [data-topic-id="${topicId}"]`
)
- ?.closest(".item");
-
- if (listItem) {
- listItem.classList.add("ai-search-result");
- }
+ ?.closest(".item")
+ ?.classList.add("ai-search-result");
}
}
});
@@ -87,16 +80,24 @@ export default class AiQuickSearch extends Component {
);
}
+ @action
+ onInsert() {
+ const resultTypes = this.search.results?.resultTypes || [];
+ const hasExistingAiResults = resultTypes.some((resultType) =>
+ resultType.results?.some((result) => result.aiGenerated)
+ );
+
+ if (hasExistingAiResults) {
+ this.lastSearchTerm = this.search.activeGlobalSearchTerm;
+ this.hasAiResults = true;
+ }
+ }
+
@action
onSearchTermChange() {
this.hasAiResults = false;
this.searchingWithAi = false;
this.lastSearchTerm = null;
-
- const resultsContainer = document.querySelector(".search-menu .results");
- if (resultsContainer) {
- resultsContainer.classList.remove("has-ai-search-results");
- }
}
@action
@@ -119,6 +120,10 @@ export default class AiQuickSearch extends Component {
this.search.noResults = false;
}
+ this.appEvents.trigger("ai-quick-search:state-changed", {
+ searching: true,
+ });
+
try {
const results = await ajax("/discourse-ai/embeddings/quick-search", {
data: {
@@ -145,6 +150,9 @@ export default class AiQuickSearch extends Component {
}
} finally {
this.searchingWithAi = false;
+ this.appEvents.trigger("ai-quick-search:state-changed", {
+ searching: false,
+ });
}
}
@@ -228,17 +236,11 @@ export default class AiQuickSearch extends Component {
<template>
<div
+ {{didInsert this.onInsert}}
{{didUpdate this.onSearchTermChange this.search.activeGlobalSearchTerm}}
{{didUpdate this.checkAndAddAiResults this.totalResults}}
{{this.markAiResults}}
>
- {{#if this.searchingWithAi}}
- <div class="ai-quick-search-loading">
- <div class="ai-quick-search-loading__content">
- {{loadingSpinner}}
- </div>
- </div>
- {{/if}}
</div>
</template>
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjs
new file mode 100644
index 00000000000..a30a381ef8e
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjs
@@ -0,0 +1,69 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { service } from "@ember/service";
+import { modifier } from "ember-modifier";
+import { i18n } from "discourse-i18n";
+
+export default class AiQuickSearchNotice extends Component {
+ static shouldRender(args, { siteSettings }) {
+ return (
+ siteSettings.ai_embeddings_semantic_quick_search_enabled &&
+ args.resultType?.type === "topic"
+ );
+ }
+
+ @service appEvents;
+
+ @tracked searching = false;
+
+ listenForAiSearch = modifier(() => {
+ const onStateChanged = ({ searching }) => {
+ this.searching = searching;
+ };
+
+ this.appEvents.on("ai-quick-search:state-changed", onStateChanged);
+
+ return () => {
+ this.appEvents.off("ai-quick-search:state-changed", onStateChanged);
+ };
+ });
+
+ get results() {
+ return this.args.outletArgs?.resultType?.results || [];
+ }
+
+ get hasAiResults() {
+ return this.results.some((result) => result.aiGenerated);
+ }
+
+ get regularResultCount() {
+ return this.results.filter((result) => !result.aiGenerated).length;
+ }
+
+ get noticeText() {
+ if (!this.hasAiResults) {
+ return null;
+ }
+
+ if (this.regularResultCount === 0) {
+ return i18n("discourse_ai.embeddings.ai_results_notice.no_results");
+ }
+ return i18n("discourse_ai.embeddings.ai_results_notice.few_results", {
+ count: this.regularResultCount,
+ });
+ }
+
+ <template>
+ <span {{this.listenForAiSearch}}>
+ {{#if this.searching}}
+ <div class="ai-quick-search-notice">
+ <div class="spinner small"></div>
+ </div>
+ {{else if this.noticeText}}
+ <div class="ai-quick-search-notice">
+ {{this.noticeText}}
+ </div>
+ {{/if}}
+ </span>
+ </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.scss
index ca0831374c8..c2262c1e0e1 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
@@ -56,6 +56,11 @@
}
}
+body.ai-semantic-search-loading .no-results-container {
+ // avoid temporary flash of 0 results
+ display: none;
+}
+
.search-results {
.fps-result {
.ai-result__icon {
@@ -108,6 +113,24 @@
}
}
+.ai-search-results-notice,
+.ai-quick-search-notice {
+ display: flex;
+ gap: var(--space-2);
+ color: var(--primary-medium);
+ font-size: var(--font-down-1);
+}
+
+.ai-search-results-notice {
+ padding: var(--space-2) 0;
+ margin-bottom: var(--space-2);
+}
+
+.ai-quick-search-notice {
+ padding: var(--space-2) var(--space-4);
+ margin-top: var(--space-1);
+}
+
// Hides other buttons and only shows loader
// while AI quick search is in progress
.search-input {
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 8080fd7b8f9..7b2a7cdb997 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1023,6 +1023,11 @@ en:
no_results: "No AI results found for this search"
semantic_search_unavailable_tooltip: "Search results must be sorted by Relevance to display AI results"
ai_generated_result: "Search result found using AI"
+ ai_results_notice:
+ no_results: "No exact matches, but here are some related results."
+ few_results:
+ one: "%{count} match found - showing related results."
+ other: "%{count} matches found - showing related results."
quick_search:
suffix: "in all topics and posts with AI"