2023-11-17 15:46:59 -05:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { on } from "@ember/modifier";
|
2023-11-21 19:51:08 -05:00
|
|
|
import { action } from "@ember/object";
|
2023-11-17 15:46:59 -05:00
|
|
|
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
|
2024-11-19 05:57:40 -05:00
|
|
|
import { service } from "@ember/service";
|
2023-11-21 19:51:08 -05:00
|
|
|
import DToggleSwitch from "discourse/components/d-toggle-switch";
|
|
|
|
import { SEARCH_TYPE_DEFAULT } from "discourse/controllers/full-page-search";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2023-11-17 15:46:59 -05:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2023-11-21 19:51:08 -05:00
|
|
|
import { isValidSearchTerm, translateResults } from "discourse/lib/search";
|
|
|
|
import icon from "discourse-common/helpers/d-icon";
|
|
|
|
import I18n from "I18n";
|
2024-09-18 12:53:54 -04:00
|
|
|
import AiIndicatorWave from "../../components/ai-indicator-wave";
|
2023-11-17 15:46:59 -05:00
|
|
|
|
|
|
|
export default class SemanticSearch extends Component {
|
|
|
|
static shouldRender(_args, { siteSettings }) {
|
|
|
|
return siteSettings.ai_embeddings_semantic_search_enabled;
|
|
|
|
}
|
|
|
|
|
2024-11-13 10:19:34 -05:00
|
|
|
@service router;
|
2023-11-17 15:46:59 -05:00
|
|
|
@service appEvents;
|
|
|
|
@service siteSettings;
|
2023-11-23 14:30:17 -05:00
|
|
|
@service searchPreferencesManager;
|
2023-11-17 15:46:59 -05:00
|
|
|
|
|
|
|
@tracked searching = false;
|
2024-11-13 10:19:34 -05:00
|
|
|
@tracked AiResults = [];
|
|
|
|
@tracked showingAiResults = false;
|
2023-11-21 19:51:08 -05:00
|
|
|
initialSearchTerm = this.args.outletArgs.search;
|
2023-11-17 15:46:59 -05:00
|
|
|
|
2023-11-20 14:37:00 -05:00
|
|
|
get disableToggleSwitch() {
|
2024-05-29 21:18:22 -04:00
|
|
|
if (
|
|
|
|
this.searching ||
|
2024-11-13 10:19:34 -05:00
|
|
|
this.AiResults.length === 0 ||
|
2024-05-29 21:18:22 -04:00
|
|
|
this.args.outletArgs.sortOrder !== 0
|
|
|
|
) {
|
2023-11-20 14:37:00 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:46:59 -05:00
|
|
|
get searchStateText() {
|
2023-12-27 19:42:13 -05:00
|
|
|
// Search results:
|
2024-11-13 10:19:34 -05:00
|
|
|
if (this.AiResults.length > 0) {
|
|
|
|
if (this.showingAiResults) {
|
2023-11-17 15:46:59 -05:00
|
|
|
return I18n.t(
|
|
|
|
"discourse_ai.embeddings.semantic_search_results.toggle",
|
|
|
|
{
|
2024-11-13 10:19:34 -05:00
|
|
|
count: this.AiResults.length,
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return I18n.t(
|
|
|
|
"discourse_ai.embeddings.semantic_search_results.toggle_hidden",
|
|
|
|
{
|
2024-11-13 10:19:34 -05:00
|
|
|
count: this.AiResults.length,
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 19:42:13 -05:00
|
|
|
|
|
|
|
// Search loading:
|
|
|
|
if (this.searching) {
|
|
|
|
return I18n.t("discourse_ai.embeddings.semantic_search_loading");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Typing to search:
|
|
|
|
if (
|
2024-11-13 10:19:34 -05:00
|
|
|
this.AiResults.length === 0 &&
|
2023-12-27 19:42:13 -05:00
|
|
|
this.searchTerm !== this.initialSearchTerm
|
|
|
|
) {
|
|
|
|
return I18n.t("discourse_ai.embeddings.semantic_search_results.new");
|
|
|
|
}
|
|
|
|
|
|
|
|
// No results:
|
2024-11-13 10:19:34 -05:00
|
|
|
if (this.AiResults.length === 0) {
|
2023-12-27 19:42:13 -05:00
|
|
|
return I18n.t("discourse_ai.embeddings.semantic_search_results.none");
|
|
|
|
}
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get searchTerm() {
|
2023-11-21 19:51:08 -05:00
|
|
|
if (this.initialSearchTerm !== this.args.outletArgs.search) {
|
|
|
|
this.initialSearchTerm = undefined;
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:46:59 -05:00
|
|
|
return this.args.outletArgs.search;
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchEnabled() {
|
|
|
|
return (
|
|
|
|
this.args.outletArgs.type === SEARCH_TYPE_DEFAULT &&
|
2024-05-29 21:18:22 -04:00
|
|
|
isValidSearchTerm(this.searchTerm, this.siteSettings) &&
|
|
|
|
this.args.outletArgs.sortOrder === 0
|
2023-11-17 15:46:59 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2024-11-13 10:19:34 -05:00
|
|
|
toggleAiResults() {
|
|
|
|
if (this.showingAiResults) {
|
2023-11-21 12:46:37 -05:00
|
|
|
this.args.outletArgs.addSearchResults([], "topic_id");
|
|
|
|
} else {
|
2024-11-13 10:19:34 -05:00
|
|
|
this.args.outletArgs.addSearchResults(this.AiResults, "topic_id");
|
2023-11-21 12:46:37 -05:00
|
|
|
}
|
2024-11-13 10:19:34 -05:00
|
|
|
this.showingAiResults = !this.showingAiResults;
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2024-11-13 10:19:34 -05:00
|
|
|
resetAiResults() {
|
|
|
|
this.AiResults = [];
|
|
|
|
this.showingAiResults = false;
|
2024-02-13 19:08:41 -05:00
|
|
|
this.args.outletArgs.addSearchResults([], "topic_id");
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2023-11-21 19:51:08 -05:00
|
|
|
handleSearch() {
|
2023-11-17 15:46:59 -05:00
|
|
|
if (!this.searchEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-23 14:30:17 -05:00
|
|
|
|
2024-01-12 14:48:07 -05:00
|
|
|
if (this.initialSearchTerm && !this.searching) {
|
2023-11-21 19:51:08 -05:00
|
|
|
return this.performHyDESearch();
|
|
|
|
}
|
|
|
|
|
2024-11-13 10:19:34 -05:00
|
|
|
this.#resetAndSearchOnEvent();
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|
2023-11-21 19:51:08 -05:00
|
|
|
|
|
|
|
performHyDESearch() {
|
|
|
|
this.searching = true;
|
2024-11-13 10:19:34 -05:00
|
|
|
this.resetAiResults();
|
2023-11-21 19:51:08 -05:00
|
|
|
|
|
|
|
ajax("/discourse-ai/embeddings/semantic-search", {
|
|
|
|
data: { q: this.searchTerm },
|
|
|
|
})
|
|
|
|
.then(async (results) => {
|
|
|
|
const model = (await translateResults(results)) || {};
|
|
|
|
|
|
|
|
if (model.posts?.length === 0) {
|
|
|
|
this.searching = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
model.posts.forEach((post) => {
|
2024-11-13 10:19:34 -05:00
|
|
|
post.generatedByAi = true;
|
2023-11-21 19:51:08 -05:00
|
|
|
});
|
|
|
|
|
2024-11-13 10:19:34 -05:00
|
|
|
this.AiResults = model.posts;
|
2023-11-21 19:51:08 -05:00
|
|
|
})
|
|
|
|
.finally(() => (this.searching = false));
|
|
|
|
}
|
|
|
|
|
2024-11-13 10:19:34 -05:00
|
|
|
#resetAndSearchOnEvent() {
|
|
|
|
return withPluginApi("1.15.0", (api) => {
|
|
|
|
api.onAppEvent("full-page-search:trigger-search", () => {
|
|
|
|
if (!this.searching) {
|
|
|
|
this.resetAiResults();
|
|
|
|
return this.performHyDESearch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
checkQueryParamsAndSearch() {
|
|
|
|
// This check is necessary because handleSearch() isn't called
|
|
|
|
// if query params are present and a new search has appended text.
|
|
|
|
// It ensures AiResults are reset and searched for properly
|
|
|
|
const searchQueryParam = this.router.currentRoute?.queryParams?.q;
|
|
|
|
if (searchQueryParam) {
|
|
|
|
this.#resetAndSearchOnEvent();
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 19:51:08 -05:00
|
|
|
<template>
|
2024-11-13 10:19:34 -05:00
|
|
|
<span {{didInsert this.checkQueryParamsAndSearch}}></span>
|
2023-11-21 19:51:08 -05:00
|
|
|
{{#if this.searchEnabled}}
|
|
|
|
<div class="semantic-search__container search-results" role="region">
|
2023-11-27 20:51:42 -05:00
|
|
|
<div class="semantic-search__results" {{didInsert this.handleSearch}}>
|
2023-11-21 19:51:08 -05:00
|
|
|
<div
|
|
|
|
class="semantic-search__searching
|
|
|
|
{{if this.searching 'in-progress'}}"
|
|
|
|
>
|
|
|
|
<DToggleSwitch
|
|
|
|
disabled={{this.disableToggleSwitch}}
|
2024-11-13 10:19:34 -05:00
|
|
|
@state={{this.showingAiResults}}
|
2023-11-21 19:51:08 -05:00
|
|
|
class="semantic-search__results-toggle"
|
2024-11-13 10:19:34 -05:00
|
|
|
{{on "click" this.toggleAiResults}}
|
2023-11-21 19:51:08 -05:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div class="semantic-search__searching-text">
|
|
|
|
{{icon "discourse-sparkles"}}
|
|
|
|
{{this.searchStateText}}
|
|
|
|
</div>
|
|
|
|
|
2024-09-18 12:53:54 -04:00
|
|
|
<AiIndicatorWave @loading={{this.searching}} />
|
2023-11-21 19:51:08 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
|
|
|
</template>
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|