2023-03-31 14:29:56 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import { translateResults, updateRecentSearches } from "discourse/lib/search";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
|
|
|
|
const SEMANTIC_SEARCH = "semantic_search";
|
|
|
|
|
|
|
|
function initializeSemanticSearch(api) {
|
|
|
|
api.addFullPageSearchType(
|
|
|
|
"discourse_ai.embeddings.semantic_search",
|
|
|
|
SEMANTIC_SEARCH,
|
2023-03-31 15:15:10 -04:00
|
|
|
(searchController, args) => {
|
2023-03-31 14:29:56 -04:00
|
|
|
if (searchController.currentUser) {
|
|
|
|
updateRecentSearches(searchController.currentUser, args.searchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
ajax("/discourse-ai/embeddings/semantic-search", { data: args })
|
|
|
|
.then(async (results) => {
|
|
|
|
const model = (await translateResults(results)) || {};
|
|
|
|
|
|
|
|
if (results.grouped_search_result) {
|
|
|
|
searchController.set("q", results.grouped_search_result.term);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.page > 1) {
|
|
|
|
if (model) {
|
|
|
|
searchController.model.posts.pushObjects(model.posts);
|
|
|
|
searchController.model.topics.pushObjects(model.topics);
|
|
|
|
searchController.model.set(
|
|
|
|
"grouped_search_result",
|
|
|
|
results.grouped_search_result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
model.grouped_search_result = results.grouped_search_result;
|
|
|
|
searchController.set("model", model);
|
|
|
|
}
|
|
|
|
searchController.set("error", null);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
searchController.set("error", e.jqXHR.responseJSON?.message);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
searchController.setProperties({
|
|
|
|
searching: false,
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2023-04-25 07:19:57 -04:00
|
|
|
name: "discourse-ai-full-page-semantic-search",
|
2023-03-31 14:29:56 -04:00
|
|
|
|
|
|
|
initialize(container) {
|
2023-04-25 07:19:57 -04:00
|
|
|
const settings = container.lookup("service:site-settings");
|
2023-04-03 10:48:38 -04:00
|
|
|
const semanticSearch = settings.ai_embeddings_semantic_search_enabled;
|
|
|
|
|
|
|
|
if (settings.ai_embeddings_enabled && semanticSearch) {
|
2023-03-31 14:29:56 -04:00
|
|
|
withPluginApi("1.6.0", initializeSemanticSearch);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|