Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions assets/javascripts/discourse/components/ai-search-discoveries.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
import { ajax } from "discourse/lib/ajax";
import { bind } from "discourse/lib/decorators";
import { withPluginApi } from "discourse/lib/plugin-api";
import { i18n } from "discourse-i18n";
import SmoothStreamer from "../lib/smooth-streamer";
import AiBlinkingAnimation from "./ai-blinking-animation";
Expand All @@ -24,6 +25,7 @@ export default class AiSearchDiscoveries extends Component {
@service appEvents;

@tracked loadingDiscoveries = false;
@tracked searching = false;
@tracked hideDiscoveries = false;
@tracked fullDiscoveryToggled = false;
@tracked discoveryPreviewLength = this.args.discoveryPreviewLength || 150;
Expand All @@ -34,8 +36,6 @@ export default class AiSearchDiscoveries extends Component {
);

discoveryTimeout = null;
typingTimer = null;
streamedTextLength = 0;

constructor() {
super(...arguments);
Expand All @@ -55,6 +55,26 @@ export default class AiSearchDiscoveries extends Component {
);
}

@bind
detectSearch() {
withPluginApi((api) => {
api.addSearchMenuOnKeyDownCallback((searchMenu, event) => {
if (!searchMenu || this.searching) {
return;
}

if (this.discobotDiscoveries.lastQuery === this.query) {
return false;
}

if (event.key === "Enter" && this.query) {
this.triggerDiscovery();
}
return true;
});
});
}

@bind
async _updateDiscovery(update) {
if (this.query === update.query) {
Expand Down Expand Up @@ -139,11 +159,14 @@ export default class AiSearchDiscoveries extends Component {

try {
this.discobotDiscoveries.lastQuery = this.query;
this.searching = true;
await ajax("/discourse-ai/ai-bot/discover", {
data: { query: this.query },
});
} catch {
this.hideDiscoveries = true;
} finally {
this.searching = false;
}
}

Expand All @@ -162,8 +185,9 @@ export default class AiSearchDiscoveries extends Component {
<template>
<div
class="ai-search-discoveries"
{{didInsert this.subscribe @searchTerm}}
{{didUpdate this.subscribe @searchTerm}}
{{didInsert this.subscribe this.query}}
{{didUpdate this.subscribe this.query}}
{{didUpdate this.detectSearch this.query}}
{{didInsert this.triggerDiscovery this.query}}
{{willDestroy this.unsubscribe}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@ export default class AiFullPageDiscobotDiscoveries extends Component {

@service discobotDiscoveries;

get hasDiscoveries() {
return this.args.outletArgs?.model?.topics?.length > 0;
}

<template>
{{#if this.hasDiscoveries}}
<h3
class="ai-search-discoveries__discoveries-title full-page-discoveries"
>
<span>
{{icon "discobot"}}
{{i18n "discourse_ai.discobot_discoveries.main_title"}}
</span>
<h3 class="ai-search-discoveries__discoveries-title full-page-discoveries">
<span>
{{icon "discobot"}}
{{i18n "discourse_ai.discobot_discoveries.main_title"}}
</span>

<AiSearchDiscoveriesTooltip />
</h3>
<div class="full-page-discoveries">
<AiSearchDiscoveries @searchTerm={{@outletArgs.search}} />
</div>
{{/if}}
<AiSearchDiscoveriesTooltip />
</h3>
<div class="full-page-discoveries">
<AiSearchDiscoveries @searchTerm={{@outletArgs.search}} />
</div>
</template>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import AiSearchDiscoveriesTooltip from "../../components/ai-search-discoveries-t
export default class AiDiscobotDiscoveries extends Component {
static shouldRender(args, { siteSettings, currentUser }) {
return (
args.resultType.type === "topic" &&
siteSettings.ai_bot_discover_persona &&
currentUser?.can_use_ai_bot_discover_persona &&
currentUser?.user_option?.ai_search_discoveries
Expand All @@ -28,7 +27,10 @@ export default class AiDiscobotDiscoveries extends Component {
<AiSearchDiscoveriesTooltip />
</h3>

<AiSearchDiscoveries @discoveryPreviewLength={{50}} />
<AiSearchDiscoveries
@searchTerm={{@outletArgs.searchTerm}}
@discoveryPreviewLength={{50}}
/>

<h3 class="ai-search-discoveries__regular-results-title">
{{icon "bars-staggered"}}
Expand Down