Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit f3e78f0

Browse files
FIX: Search discoveries improvements (#1228)
**This update makes a few improvements to search discoveries:** - [x] in search menu panel: search discoveries should still be triggered when no regular results are present - [x] in full page search: search discoveries should still be triggered when no regular results are present - [x] flakiness in search discoveries sometimes not working properly. --------- Co-authored-by: awesomerobot <[email protected]>
1 parent 28fa723 commit f3e78f0

File tree

5 files changed

+94
-44
lines changed

5 files changed

+94
-44
lines changed

assets/javascripts/discourse/components/ai-search-discoveries.gjs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import DButton from "discourse/components/d-button";
1111
import concatClass from "discourse/helpers/concat-class";
1212
import { ajax } from "discourse/lib/ajax";
1313
import { bind } from "discourse/lib/decorators";
14+
import { withPluginApi } from "discourse/lib/plugin-api";
1415
import { i18n } from "discourse-i18n";
1516
import SmoothStreamer from "../lib/smooth-streamer";
1617
import AiBlinkingAnimation from "./ai-blinking-animation";
@@ -23,7 +24,6 @@ export default class AiSearchDiscoveries extends Component {
2324
@service discobotDiscoveries;
2425
@service appEvents;
2526

26-
@tracked loadingDiscoveries = false;
2727
@tracked hideDiscoveries = false;
2828
@tracked fullDiscoveryToggled = false;
2929
@tracked discoveryPreviewLength = this.args.discoveryPreviewLength || 150;
@@ -34,8 +34,6 @@ export default class AiSearchDiscoveries extends Component {
3434
);
3535

3636
discoveryTimeout = null;
37-
typingTimer = null;
38-
streamedTextLength = 0;
3937

4038
constructor() {
4139
super(...arguments);
@@ -55,6 +53,34 @@ export default class AiSearchDiscoveries extends Component {
5553
);
5654
}
5755

56+
@bind
57+
detectSearch() {
58+
if (
59+
this.query?.length === 0 &&
60+
this.discobotDiscoveries.discovery?.length > 0
61+
) {
62+
this.discobotDiscoveries.resetDiscovery();
63+
this.smoothStreamer.resetStreaming();
64+
}
65+
66+
withPluginApi((api) => {
67+
api.addSearchMenuOnKeyDownCallback((searchMenu, event) => {
68+
if (!searchMenu || this.discobotDiscoveries.loadingDiscoveries) {
69+
return;
70+
}
71+
72+
if (this.discobotDiscoveries.lastQuery === this.query) {
73+
return false;
74+
}
75+
76+
if (event.key === "Enter" && this.query) {
77+
this.triggerDiscovery();
78+
}
79+
return true;
80+
});
81+
});
82+
}
83+
5884
@bind
5985
async _updateDiscovery(update) {
6086
if (this.query === update.query) {
@@ -67,7 +93,7 @@ export default class AiSearchDiscoveries extends Component {
6793
}
6894

6995
this.discobotDiscoveries.modelUsed = update.model_used;
70-
this.loadingDiscoveries = false;
96+
this.discobotDiscoveries.loadingDiscoveries = false;
7197
this.smoothStreamer.updateResult(update, "ai_discover_reply");
7298
}
7399
}
@@ -110,7 +136,7 @@ export default class AiSearchDiscoveries extends Component {
110136

111137
get canShowExpandtoggle() {
112138
return (
113-
!this.loadingDiscoveries &&
139+
!this.discobotDiscoveries.loadingDiscoveries &&
114140
this.smoothStreamer.renderedText.length > this.discoveryPreviewLength
115141
);
116142
}
@@ -130,7 +156,8 @@ export default class AiSearchDiscoveries extends Component {
130156
}
131157

132158
this.hideDiscoveries = false;
133-
this.loadingDiscoveries = true;
159+
this.discobotDiscoveries.loadingDiscoveries = true;
160+
134161
this.discoveryTimeout = later(
135162
this,
136163
this.timeoutDiscovery,
@@ -139,6 +166,7 @@ export default class AiSearchDiscoveries extends Component {
139166

140167
try {
141168
this.discobotDiscoveries.lastQuery = this.query;
169+
142170
await ajax("/discourse-ai/ai-bot/discover", {
143171
data: { query: this.query },
144172
});
@@ -153,7 +181,7 @@ export default class AiSearchDiscoveries extends Component {
153181
}
154182

155183
timeoutDiscovery() {
156-
this.loadingDiscoveries = false;
184+
this.discobotDiscoveries.loadingDiscoveries = false;
157185
this.discobotDiscoveries.discovery = "";
158186

159187
this.discobotDiscoveries.discoveryTimedOut = true;
@@ -162,13 +190,14 @@ export default class AiSearchDiscoveries extends Component {
162190
<template>
163191
<div
164192
class="ai-search-discoveries"
165-
{{didInsert this.subscribe @searchTerm}}
166-
{{didUpdate this.subscribe @searchTerm}}
193+
{{didInsert this.subscribe this.query}}
194+
{{didUpdate this.subscribe this.query}}
195+
{{didUpdate this.detectSearch this.query}}
167196
{{didInsert this.triggerDiscovery this.query}}
168197
{{willDestroy this.unsubscribe}}
169198
>
170199
<div class="ai-search-discoveries__completion">
171-
{{#if this.loadingDiscoveries}}
200+
{{#if this.discobotDiscoveries.loadingDiscoveries}}
172201
<AiBlinkingAnimation />
173202
{{else if this.discobotDiscoveries.discoveryTimedOut}}
174203
{{i18n "discourse_ai.discobot_discoveries.timed_out"}}
@@ -181,9 +210,10 @@ export default class AiSearchDiscoveries extends Component {
181210
"streamable-content"
182211
}}
183212
>
184-
<div class="cooked">
185-
<CookText @rawText={{this.smoothStreamer.renderedText}} />
186-
</div>
213+
<CookText
214+
@rawText={{this.smoothStreamer.renderedText}}
215+
class="cooked"
216+
/>
187217
</article>
188218

189219
{{#if this.canShowExpandtoggle}}

assets/javascripts/discourse/connectors/full-page-search-below-search-header/ai-full-page-discobot-discoveries.gjs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ export default class AiFullPageDiscobotDiscoveries extends Component {
1616

1717
@service discobotDiscoveries;
1818

19-
get hasDiscoveries() {
20-
return this.args.outletArgs?.model?.topics?.length > 0;
21-
}
22-
2319
<template>
24-
{{#if this.hasDiscoveries}}
20+
{{#if this.discobotDiscoveries.showDiscoveryTitle}}
2521
<h3
2622
class="ai-search-discoveries__discoveries-title full-page-discoveries"
2723
>
@@ -32,9 +28,10 @@ export default class AiFullPageDiscobotDiscoveries extends Component {
3228

3329
<AiSearchDiscoveriesTooltip />
3430
</h3>
35-
<div class="full-page-discoveries">
36-
<AiSearchDiscoveries @searchTerm={{@outletArgs.search}} />
37-
</div>
3831
{{/if}}
32+
33+
<div class="full-page-discoveries">
34+
<AiSearchDiscoveries @searchTerm={{@outletArgs.search}} />
35+
</div>
3936
</template>
4037
}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,39 @@ import AiSearchDiscoveriesTooltip from "../../components/ai-search-discoveries-t
88
export default class AiDiscobotDiscoveries extends Component {
99
static shouldRender(args, { siteSettings, currentUser }) {
1010
return (
11-
args.resultType.type === "topic" &&
1211
siteSettings.ai_bot_discover_persona &&
1312
currentUser?.can_use_ai_bot_discover_persona &&
1413
currentUser?.user_option?.ai_search_discoveries
1514
);
1615
}
1716

1817
@service discobotDiscoveries;
18+
@service search;
1919

2020
<template>
2121
<div class="ai-discobot-discoveries">
22-
<h3 class="ai-search-discoveries__discoveries-title">
23-
<span>
24-
{{icon "discobot"}}
25-
{{i18n "discourse_ai.discobot_discoveries.main_title"}}
26-
</span>
22+
{{#if this.discobotDiscoveries.showDiscoveryTitle}}
23+
<h3 class="ai-search-discoveries__discoveries-title">
24+
<span>
25+
{{icon "discobot"}}
26+
{{i18n "discourse_ai.discobot_discoveries.main_title"}}
27+
</span>
2728

28-
<AiSearchDiscoveriesTooltip />
29-
</h3>
29+
<AiSearchDiscoveriesTooltip />
30+
</h3>
31+
{{/if}}
3032

31-
<AiSearchDiscoveries @discoveryPreviewLength={{50}} />
33+
<AiSearchDiscoveries
34+
@searchTerm={{@outletArgs.searchTerm}}
35+
@discoveryPreviewLength={{50}}
36+
/>
3237

33-
<h3 class="ai-search-discoveries__regular-results-title">
34-
{{icon "bars-staggered"}}
35-
{{i18n "discourse_ai.discobot_discoveries.regular_results"}}
36-
</h3>
38+
{{#if this.search.results.topics.length}}
39+
<h3 class="ai-search-discoveries__regular-results-title">
40+
{{icon "bars-staggered"}}
41+
{{i18n "discourse_ai.discobot_discoveries.regular_results"}}
42+
</h3>
43+
{{/if}}
3744
</div>
3845
</template>
3946
}

assets/javascripts/discourse/services/discobot-discoveries.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ export default class DiscobotDiscoveries extends Service {
1111
@tracked lastQuery = "";
1212
@tracked discoveryTimedOut = false;
1313
@tracked modelUsed = "";
14+
@tracked loadingDiscoveries = false;
1415

1516
resetDiscovery() {
17+
this.loadingDiscoveries = false;
1618
this.discovery = "";
1719
this.discoveryTimedOut = false;
1820
this.modelUsed = "";
1921
}
2022

23+
get showDiscoveryTitle() {
24+
return this.discovery.length > 0 || this.loadingDiscoveries;
25+
}
26+
2127
@action
2228
async disableDiscoveries() {
2329
this.currentUser.user_option.ai_search_discoveries = false;

assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
.ai-search-discoveries {
1212
&__regular-results-title {
13+
margin-top: 0.5em;
1314
margin-bottom: 0;
1415
}
1516

@@ -93,10 +94,6 @@
9394
}
9495
}
9596

96-
.ai-discobot-discoveries {
97-
padding: 0.5em;
98-
}
99-
10097
.full-page-discoveries {
10198
padding: 1em 10%;
10299
}
@@ -107,30 +104,43 @@
107104
width: 1.15em;
108105
}
109106

107+
.ai-discobot-discoveries {
108+
padding-top: 0.5em;
109+
}
110+
110111
@include breakpoint("medium", min-width) {
111-
.search-menu .menu-panel:has(.ai-discobot-discoveries) {
112+
.search-menu .menu-panel:has(.ai-search-discoveries__discoveries-title) {
112113
width: 80vw;
113114
max-width: 800px;
114115
transition: width 0.5s;
115116

116-
.search-result-topic {
117+
.results {
117118
display: grid;
118-
grid-template-areas: "results-title ai-title" "results ai";
119119
grid-template-columns: 58% 38%;
120+
grid-template-rows: auto auto 1fr;
120121
gap: 0 4%;
121122

122-
.list {
123-
grid-area: results;
123+
* {
124+
// covers all non-discovery content
125+
grid-column-start: 1;
124126
}
125127

126128
.ai-discobot-discoveries {
127-
grid-area: ai;
129+
// always in the second column, always spans all rows
130+
grid-column-start: 2;
131+
grid-row: 1 / -1;
132+
padding-top: 0;
128133
}
129134
}
130135

131136
.ai-search-discoveries {
132137
font-size: var(--font-0);
133138
color: var(--primary-high);
139+
padding-right: 0.5em;
140+
}
141+
142+
.ai-search-discoveries__discoveries-title {
143+
margin-top: 0.5em;
134144
}
135145

136146
.ai-search-discoveries__regular-results-title {

0 commit comments

Comments
 (0)