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";
|
2023-11-21 19:51:08 -05:00
|
|
|
import { inject as service } from "@ember/service";
|
|
|
|
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 { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
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";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@service appEvents;
|
|
|
|
@service siteSettings;
|
|
|
|
|
|
|
|
@tracked searching = false;
|
|
|
|
@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() {
|
|
|
|
if (this.searching || this.AIResults.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:46:59 -05:00
|
|
|
get searchStateText() {
|
|
|
|
if (this.searching) {
|
|
|
|
return I18n.t("discourse_ai.embeddings.semantic_search_loading");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.AIResults.length === 0) {
|
|
|
|
return I18n.t("discourse_ai.embeddings.semantic_search_results.none");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.AIResults.length > 0) {
|
|
|
|
if (this.showingAIResults) {
|
|
|
|
return I18n.t(
|
|
|
|
"discourse_ai.embeddings.semantic_search_results.toggle",
|
|
|
|
{
|
|
|
|
count: this.AIResults.length,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return I18n.t(
|
|
|
|
"discourse_ai.embeddings.semantic_search_results.toggle_hidden",
|
|
|
|
{
|
|
|
|
count: this.AIResults.length,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchTerm() {
|
2023-11-21 19:51:08 -05:00
|
|
|
if (this.initialSearchTerm !== this.args.outletArgs.search) {
|
|
|
|
this.initialSearchTerm = undefined;
|
|
|
|
this.resetAIResults();
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:46:59 -05:00
|
|
|
return this.args.outletArgs.search;
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchEnabled() {
|
|
|
|
return (
|
|
|
|
this.args.outletArgs.type === SEARCH_TYPE_DEFAULT &&
|
|
|
|
isValidSearchTerm(this.searchTerm, this.siteSettings)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleAIResults() {
|
2023-11-21 12:46:37 -05:00
|
|
|
if (this.showingAIResults) {
|
|
|
|
this.args.outletArgs.addSearchResults([], "topic_id");
|
|
|
|
} else {
|
|
|
|
this.args.outletArgs.addSearchResults(this.AIResults, "topic_id");
|
|
|
|
}
|
2023-11-17 15:46:59 -05:00
|
|
|
this.showingAIResults = !this.showingAIResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
resetAIResults() {
|
|
|
|
this.AIResults = [];
|
|
|
|
this.showingAIResults = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2023-11-21 19:51:08 -05:00
|
|
|
handleSearch() {
|
2023-11-17 15:46:59 -05:00
|
|
|
if (!this.searchEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-21 19:51:08 -05:00
|
|
|
if (this.initialSearchTerm) {
|
|
|
|
return this.performHyDESearch();
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:46:59 -05:00
|
|
|
withPluginApi("1.15.0", (api) => {
|
|
|
|
api.onAppEvent("full-page-search:trigger-search", () => {
|
2023-11-21 19:51:08 -05:00
|
|
|
return this.performHyDESearch();
|
2023-11-17 15:46:59 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-11-21 19:51:08 -05:00
|
|
|
|
|
|
|
performHyDESearch() {
|
|
|
|
this.searching = true;
|
|
|
|
this.resetAIResults();
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
post.generatedByAI = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.AIResults = model.posts;
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => (this.searching = false));
|
|
|
|
}
|
|
|
|
|
|
|
|
<template>
|
|
|
|
{{#if this.searchEnabled}}
|
|
|
|
<div class="semantic-search__container search-results" role="region">
|
|
|
|
<div class="semantic-search__results" {{didInsert this.handleSearch}}>
|
|
|
|
<div
|
|
|
|
class="semantic-search__searching
|
|
|
|
{{if this.searching 'in-progress'}}"
|
|
|
|
>
|
|
|
|
<DToggleSwitch
|
|
|
|
disabled={{this.disableToggleSwitch}}
|
|
|
|
@state={{this.showingAIResults}}
|
|
|
|
title="AI search results hidden"
|
|
|
|
class="semantic-search__results-toggle"
|
|
|
|
{{on "click" this.toggleAIResults}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div class="semantic-search__searching-text">
|
|
|
|
{{icon "discourse-sparkles"}}
|
|
|
|
{{this.searchStateText}}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{{#if this.searching}}
|
|
|
|
<span class="semantic-search__indicator-wave">
|
|
|
|
<span class="semantic-search__indicator-dot">.</span>
|
|
|
|
<span class="semantic-search__indicator-dot">.</span>
|
|
|
|
<span class="semantic-search__indicator-dot">.</span>
|
|
|
|
</span>
|
|
|
|
{{/if}}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
|
|
|
</template>
|
2023-11-17 15:46:59 -05:00
|
|
|
}
|