Skip to content

A platform for community discussion. Free, open, simple.

UX: add AI inline to topic title edit (#41329)

chapoi5d3294f9e385
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs
deleted file mode 100644
index 87707a6125f..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-category-suggester.gjs
+++ /dev/null
@@ -1,159 +0,0 @@
-import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { array, fn } from "@ember/helper";
-import { on } from "@ember/modifier";
-import { action } from "@ember/object";
-import { service } from "@ember/service";
-import DMenu from "discourse/float-kit/components/d-menu";
-import { ajax } from "discourse/lib/ajax";
-import { popupAjaxError } from "discourse/lib/ajax-error";
-import DButton from "discourse/ui-kit/d-button";
-import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
-import dCategoryBadge from "discourse/ui-kit/helpers/d-category-badge";
-import { i18n } from "discourse-i18n";
-import {
- MIN_CHARACTER_COUNT,
- showSuggestionsError,
-} from "../../lib/ai-helper-suggestions";
-
-export default class AiCategorySuggester extends Component {
- @service siteSettings;
-
- @tracked loading = false;
- @tracked suggestions = null;
- @tracked triggerIcon = "discourse-sparkles";
- dMenu;
-
- get content() {
- return this.args.composer?.reply;
- }
-
- get showSuggestionButton() {
- const showTrigger =
- this.content?.length > MIN_CHARACTER_COUNT ||
- this.args.topicState === "edit";
-
- document
- .querySelector(".composer-fields")
- ?.classList.toggle("showing-ai-suggestions", showTrigger);
-
- return this.siteSettings.ai_embeddings_enabled && showTrigger;
- }
-
- get showDropdown() {
- return !this.loading && this.suggestions?.length > 0;
- }
-
- @action
- async loadSuggestions() {
- if (this.suggestions?.length > 0 && !this.dMenu.expanded) {
- return this.suggestions;
- }
-
- this.loading = true;
- this.triggerIcon = "spinner";
-
- const data = {};
-
- if (this.content) {
- data.text = this.content;
- } else {
- data.topic_id = this.args.buffered.content.id;
- }
-
- try {
- const { assistant } = await ajax(
- "/discourse-ai/ai-helper/suggest_category",
- { method: "POST", data }
- );
-
- this.suggestions = assistant;
-
- if (this.suggestions?.length === 0) {
- this.dMenu.close();
- showSuggestionsError(this, this.loadSuggestions.bind(this));
- return;
- }
- } catch (error) {
- popupAjaxError(error);
- } finally {
- this.loading = false;
- this.triggerIcon = "rotate";
- }
-
- return this.suggestions;
- }
-
- @action
- applySuggestion(suggestion) {
- if (this.args.composer) {
- this.args.composer.set("categoryId", suggestion.id);
- }
-
- if (this.args.buffered) {
- this.args.buffered.set("category_id", suggestion.id);
- }
-
- return this.dMenu.close();
- }
-
- @action
- onRegisterApi(api) {
- this.dMenu = api;
- }
-
- @action
- onClose() {
- if (this.suggestions?.length > 0) {
- // If all suggestions have been used,
- // re-triggering when no suggestions present
- // will cause computation issues with
- // setting the icon, so we prevent it
- this.triggerIcon = "discourse-sparkles";
- }
- }
-
- <template>
- {{#if this.showSuggestionButton}}
- <DMenu
- @title={{i18n "discourse_ai.ai_helper.suggest"}}
- @icon={{this.triggerIcon}}
- @identifier="ai-category-suggester"
- @onClose={{this.onClose}}
- @triggerClass="btn-transparent suggestion-button suggest-category-button {{if
- this.loading
- 'is-loading'
- }}"
- @contentClass="ai-suggestions-menu"
- @onRegisterApi={{this.onRegisterApi}}
- @modalForMobile={{true}}
- @untriggers={{array}}
- {{on "click" this.loadSuggestions}}
- >
- <:content>
- {{#if this.showDropdown}}
- <DDropdownMenu as |dropdown|>
- {{#each this.suggestions as |suggestion index|}}
- <dropdown.item>
- <DButton
- class="category-row"
- data-name={{suggestion.name}}
- data-value={{index}}
- title={{suggestion.name}}
- @action={{fn this.applySuggestion suggestion}}
- >
- <div class="category-status">
- {{dCategoryBadge suggestion}}
- <span class="topic-count">x
- {{suggestion.topicCount}}</span>
- </div>
- </DButton>
- </dropdown.item>
- {{/each}}
- </DDropdownMenu>
- {{/if}}
- </:content>
- </DMenu>
- {{/if}}
- </template>
-}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs
deleted file mode 100644
index 8353698e12a..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-tag-suggester.gjs
+++ /dev/null
@@ -1,209 +0,0 @@
-import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { array, fn } from "@ember/helper";
-import { on } from "@ember/modifier";
-import { action } from "@ember/object";
-import { service } from "@ember/service";
-import DMenu from "discourse/float-kit/components/d-menu";
-import { ajax } from "discourse/lib/ajax";
-import { popupAjaxError } from "discourse/lib/ajax-error";
-import DButton from "discourse/ui-kit/d-button";
-import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
-import dDiscourseTag from "discourse/ui-kit/helpers/d-discourse-tag";
-import { i18n } from "discourse-i18n";
-import {
- MIN_CHARACTER_COUNT,
- showSuggestionsError,
- tagSuggestionParams,
-} from "../../lib/ai-helper-suggestions";
-
-export default class AiTagSuggester extends Component {
- @service siteSettings;
- @service toasts;
- @service composer;
-
- @tracked loading = false;
- @tracked suggestions = null;
- @tracked triggerIcon = "discourse-sparkles";
- dMenu;
-
- get content() {
- return this.args.composer?.reply;
- }
-
- get model() {
- return this.args.composer || this.args.buffered;
- }
-
- get showSuggestionButton() {
- if (this.composer.disableTagsChooser) {
- return false;
- }
-
- const showTrigger =
- this.content?.length > MIN_CHARACTER_COUNT ||
- this.args.topicState === "edit";
-
- document
- .querySelector(".composer-fields")
- ?.classList.toggle("showing-ai-suggestions", showTrigger);
-
- return this.siteSettings.ai_embeddings_enabled && showTrigger;
- }
-
- get showDropdown() {
- return !this.loading && this.suggestions?.length > 0;
- }
-
- get tagSelectorHasValues() {
- return this.model.get("tags")?.length > 0;
- }
-
- @action
- async loadSuggestions() {
- if (this.suggestions?.length > 0 && !this.dMenu.expanded) {
- return this.suggestions;
- }
-
- this.loading = true;
- this.triggerIcon = "spinner";
-
- const data = {};
-
- if (this.content) {
- data.text = this.content;
- Object.assign(
- data,
- tagSuggestionParams(this.model?.categoryId, this.model?.get("tags"))
- );
- } else {
- data.topic_id = this.args.buffered.content.id;
- }
-
- try {
- const { assistant } = await ajax("/discourse-ai/ai-helper/suggest_tags", {
- method: "POST",
- data,
- });
-
- this.suggestions = assistant;
-
- if (this.tagSelectorHasValues) {
- this.suggestions = this.suggestions.filter(
- (s) => !this.model.get("tags").some((t) => t.id === s.id)
- );
- }
-
- if (this.suggestions?.length === 0) {
- showSuggestionsError(this, this.loadSuggestions.bind(this));
- return;
- }
- } catch (error) {
- popupAjaxError(error);
- } finally {
- this.loading = false;
- this.triggerIcon = "rotate";
- }
-
- return this.suggestions;
- }
-
- #removeAppliedTag(suggestion) {
- this.suggestions = this.suggestions.filter((s) => s.id !== suggestion.id);
- }
-
- @action
- applySuggestion(suggestion) {
- const maxTags = this.siteSettings.max_tags_per_topic;
- if (!this.model) {
- return;
- }
-
- const tags = this.model.get("tags");
-
- if (!tags) {
- this.model.set("tags", [suggestion]);
- this.#removeAppliedTag(suggestion);
- return;
- }
-
- if (tags.length >= maxTags) {
- return this.toasts.error({
- class: "ai-suggestion-error",
- duration: "short",
- data: {
- message: i18n("discourse_ai.ai_helper.suggest_errors.too_many_tags", {
- count: maxTags,
- }),
- },
- });
- }
-
- tags.push(suggestion);
- this.model.set("tags", [...tags]);
- suggestion.disabled = true;
- this.#removeAppliedTag(suggestion);
- }
-
- @action
- onRegisterApi(api) {
- this.dMenu = api;
- }
-
- @action
- onClose() {
- if (this.suggestions?.length > 0) {
- // If all suggestions have been used,
- // re-triggering when no suggestions present
- // will cause computation issues with
- // setting the icon, so we prevent it
- this.triggerIcon = "discourse-sparkles";
- }
- }
-
- <template>
- {{#if this.showSuggestionButton}}
- <DMenu
- @title={{i18n "discourse_ai.ai_helper.suggest"}}
- @icon={{this.triggerIcon}}
- @identifier="ai-tag-suggester"
- @onClose={{this.onClose}}
- @triggerClass="btn-transparent suggestion-button suggest-tags-button {{if
- this.loading
- 'is-loading'
- }}"
- @contentClass="ai-suggestions-menu"
- @onRegisterApi={{this.onRegisterApi}}
- @modalForMobile={{true}}
- @untriggers={{array}}
- {{on "click" this.loadSuggestions}}
- >
- <:content>
- {{#if this.showDropdown}}
- <DDropdownMenu as |dropdown|>
- {{#each this.suggestions as |suggestion index|}}
- <dropdown.item>
- <DButton
- class="tag-row"
- data-name={{suggestion.name}}
- data-value={{index}}
- title={{suggestion.name}}
- @translatedLabel={{suggestion.name}}
- @disabled={{suggestion.disabled}}
- @action={{fn this.applySuggestion suggestion}}
- >
- {{dDiscourseTag
- suggestion.name
- count=suggestion.count
- noHref=true
- }}
- </DButton>
- </dropdown.item>
- {{/each}}
- </DDropdownMenu>
- {{/if}}
- </:content>
- </DMenu>
- {{/if}}
- </template>
-}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs
index fea66aa48ab..f1fb648e8d3 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs
@@ -123,7 +123,7 @@ export default class AiTitleSuggester extends Component {
@icon={{this.triggerIcon}}
@identifier="ai-title-suggester"
@onClose={{this.onClose}}
- @triggerClass="btn-transparent suggestion-button suggest-titles-button {{if
+ @triggerClass="suggestion-button suggest-titles-button {{if
this.loading
'is-loading'
}}"
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/edit-topic-category__after/ai-category-suggestion.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/edit-topic-category__after/ai-category-suggestion.gjs
deleted file mode 100644
index 2227beafe13..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/edit-topic-category__after/ai-category-suggestion.gjs
+++ /dev/null
@@ -1,21 +0,0 @@
-import Component from "@glimmer/component";
-import AiCategorySuggester from "../../components/suggestion-menus/ai-category-suggester";
-import { showComposerAiHelper } from "../../lib/show-ai-helper";
-
-export default class AiCategorySuggestion extends Component {
- static shouldRender(args, context) {
- return showComposerAiHelper(
- args?.composer,
- context.siteSettings,
- context.currentUser,
- "suggestions"
- );
- }
-
- <template>
- <AiCategorySuggester
- @buffered={{@outletArgs.buffered}}
- @topicState="edit"
- />
- </template>
-}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/edit-topic-tags__after/ai-tag-suggestion.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/edit-topic-tags__after/ai-tag-suggestion.gjs
deleted file mode 100644
index aef1a2bc300..00000000000
--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/edit-topic-tags__after/ai-tag-suggestion.gjs
+++ /dev/null
@@ -1,18 +0,0 @@
-import Component from "@glimmer/component";
-import AiTagSuggester from "../../components/suggestion-menus/ai-tag-suggester";
-import { showComposerAiHelper } from "../../lib/show-ai-helper";
-
-export default class AiCategorySuggestion extends Component {
- static shouldRender(args, context) {
- return showComposerAiHelper(
- args?.composer,
- context.siteSettings,
- context.currentUser,
- "suggestions"
- );
- }
-
- <template>
- <AiTagSuggester @buffered={{@outletArgs.buffered}} @topicState="edit" />
- </template>
-}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-category-chooser-inline-suggester.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-category-chooser-inline-suggester.js
index fd0dbc68f91..bec5282ebc2 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-category-chooser-inline-suggester.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-category-chooser-inline-suggester.js
@@ -3,7 +3,7 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { withPluginApi } from "discourse/lib/plugin-api";
import { i18n } from "discourse-i18n";
-import { MIN_CHARACTER_COUNT } from "../lib/ai-helper-suggestions";
+import { chooserSuggestionContext } from "../lib/chooser-suggestion-context";
import { showComposerAiHelper } from "../lib/show-ai-helper";
const SUGGEST_ID = "ai-category-suggest";
@@ -19,16 +19,6 @@ function stateFor(component) {
return state;
}
-function composerFor(component) {
- return getOwner(component).lookup("service:composer");
-}
-
-// only attach to the composer's own chooser, not every category-chooser
-// elsewhere in the app while a draft happens to be open
-function inComposer(component) {
- return !!component.element?.closest("#reply-control");
-}
-
function noSuggestionsToast(component) {
getOwner(component)
.lookup("service:toasts")
@@ -40,17 +30,16 @@ function noSuggestionsToast(component) {
});
}
-async function applyBestSuggestion(component, selectKit) {
- const composer = composerFor(component);
- const text = composer?.model?.reply;
+async function applyBestSuggestion(component, selectKit, context) {
const state = stateFor(component);
+ const data = context.categoryRequestData();
- if (!text || text.length < MIN_CHARACTER_COUNT) {
+ if (!data) {
noSuggestionsToast(component);
return;
}
- const startCategoryId = composer.model.categoryId;
+ const startValue = selectKit.value;
state.loading = true;
selectKit.close();
selectKit.set("isLoading", true);
@@ -58,14 +47,14 @@ async function applyBestSuggestion(component, selectKit) {
try {
const { assistant } = await ajax(
"/discourse-ai/ai-helper/suggest_category",
- { method: "POST", data: { text } }
+ { method: "POST", data }
);
if (component.isDestroying || component.isDestroyed) {
return;
}
- if (composer.model.categoryId !== startCategoryId) {
+ if (selectKit.value !== startValue) {
return;
}
@@ -85,25 +74,23 @@ async function applyBestSuggestion(component, selectKit) {
}
}
-function triggerRow(component) {
+function triggerRow(component, context) {
return {
id: SUGGEST_ID,
name: i18n("discourse_ai.ai_helper.choose"),
icon: "discourse-sparkles",
classNames: "ai-category-suggest-row",
- onSelect: (selectKit) => applyBestSuggestion(component, selectKit),
+ onSelect: (selectKit) => applyBestSuggestion(component, selectKit, context),
};
}
-function enabledFor(component, siteSettings, currentUser) {
- const composer = composerFor(component);
+function enabledFor(context, siteSettings, currentUser) {
return (
+ !!context &&
siteSettings.ai_embeddings_enabled &&
- composer?.model &&
- !composer.disableCategoryChooser &&
- inComposer(component) &&
+ context.categoryChooserEnabled &&
showComposerAiHelper(
- composer.model,
+ context.model,
siteSettings,
currentUser,
"suggestions"
@@ -111,18 +98,13 @@ function enabledFor(component, siteSettings, currentUser) {
);
}
-function hasEnoughContent(component) {
- return (
- (composerFor(component)?.model?.reply?.length ?? 0) > MIN_CHARACTER_COUNT
- );
-}
-
function initInlineCategorySuggester(api) {
const currentUser = api.getCurrentUser();
const siteSettings = api.container.lookup("service:site-settings");
api.modifySelectKit("category-chooser").prependContent((component) => {
- if (!enabledFor(component, siteSettings, currentUser)) {
+ const context = chooserSuggestionContext(component);
+ if (!enabledFor(context, siteSettings, currentUser)) {
return;
}
@@ -130,8 +112,8 @@ function initInlineCategorySuggester(api) {
return;
}
- if (!stateFor(component).loading && hasEnoughContent(component)) {
- return triggerRow(component);
+ if (!stateFor(component).loading && context.available) {
+ return triggerRow(component, context);
}
});
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tag-chooser-inline-suggester.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tag-chooser-inline-suggester.js
index 8df5edda0d9..a1a71face69 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tag-chooser-inline-suggester.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tag-chooser-inline-suggester.js
@@ -3,10 +3,7 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { withPluginApi } from "discourse/lib/plugin-api";
import { i18n } from "discourse-i18n";
-import {
- MIN_CHARACTER_COUNT,
- tagSuggestionParams,
-} from "../lib/ai-helper-suggestions";
+import { chooserSuggestionContext } from "../lib/chooser-suggestion-context";
import { showComposerAiHelper } from "../lib/show-ai-helper";
const SUGGEST_ID = "ai-tag-suggest";
@@ -29,16 +26,6 @@ function reset(component) {
state.suggestions = [];
}
-function composerFor(component) {
- return getOwner(component).lookup("service:composer");
-}
-
-// only attach to the composer's own chooser, not every mini-tag-chooser
-// elsewhere in the app while a draft happens to be open
-function inComposer(component) {
- return !!component.element?.closest("#reply-control");
-}
-
function noSuggestionsToast(component) {
getOwner(component)
.lookup("service:toasts")
@@ -50,18 +37,11 @@ function noSuggestionsToast(component) {
});
}
-function selectedTagNames(component) {
- const tags = composerFor(component)?.model?.tags ?? [];
- return tags.map((t) => (typeof t === "string" ? t : t.name));
-}
-
-async function loadSuggestions(component, selectKit) {
- const composer = composerFor(component);
- const model = composer?.model;
- const text = model?.reply;
+async function loadSuggestions(component, selectKit, context) {
const state = stateFor(component);
+ const data = context.tagRequestData();
- if (!text || text.length < MIN_CHARACTER_COUNT) {
+ if (!data) {
noSuggestionsToast(component);
return;
}
@@ -72,7 +52,7 @@ async function loadSuggestions(component, selectKit) {
try {
const { assistant } = await ajax("/discourse-ai/ai-helper/suggest_tags", {
method: "POST",
- data: { text, ...tagSuggestionParams(model.categoryId, model.tags) },
+ data,
});
if (component.isDestroying || component.isDestroyed) {
@@ -95,13 +75,13 @@ async function loadSuggestions(component, selectKit) {
}
}
-function triggerRow(component) {
+function triggerRow(component, context) {
return {
id: SUGGEST_ID,
name: i18n("discourse_ai.ai_helper.suggest"),
icon: "discourse-sparkles",
classNames: "ai-tag-suggest-row",
- onSelect: (selectKit) => loadSuggestions(component, selectKit),
+ onSelect: (selectKit) => loadSuggestions(component, selectKit, context),
};
}
@@ -128,15 +108,13 @@ function exitRow(component) {
};
}
-function enabledFor(component, siteSettings, currentUser) {
- const composer = composerFor(component);
+function enabledFor(context, siteSettings, currentUser) {
return (
+ !!context &&
siteSettings.ai_embeddings_enabled &&
- composer?.model &&
- !composer.disableTagsChooser &&
- inComposer(component) &&
+ context.tagChooserEnabled &&
showComposerAiHelper(
- composer.model,
+ context.model,
siteSettings,
currentUser,
"suggestions"
@@ -144,18 +122,13 @@ function enabledFor(component, siteSettings, currentUser) {
);
}
-function hasEnoughContent(component) {
- return (
- (composerFor(component)?.model?.reply?.length ?? 0) > MIN_CHARACTER_COUNT
- );
-}
-
function initInlineTagSuggester(api) {
const currentUser = api.getCurrentUser();
const siteSettings = api.container.lookup("service:site-settings");
api.modifySelectKit("mini-tag-chooser").prependContent((component) => {
- if (!enabledFor(component, siteSettings, currentUser)) {
+ const context = chooserSuggestionContext(component);
+ if (!enabledFor(context, siteSettings, currentUser)) {
return;
}
@@ -164,13 +137,14 @@ function initInlineTagSuggester(api) {
return;
}
- if (stateFor(component).mode === "idle" && hasEnoughContent(component)) {
- return triggerRow(component);
+ if (stateFor(component).mode === "idle" && context.available) {
+ return triggerRow(component, context);
}
});
api.modifySelectKit("mini-tag-chooser").replaceContent((component) => {
- if (!enabledFor(component, siteSettings, currentUser)) {
+ const context = chooserSuggestionContext(component);
+ if (!enabledFor(context, siteSettings, currentUser)) {
return;
}
@@ -185,7 +159,7 @@ function initInlineTagSuggester(api) {
}
if (state.mode === "results") {
- const taken = selectedTagNames(component);
+ const taken = context.selectedTagNames;
const remaining = state.suggestions.filter(
(s) => !taken.includes(s.name)
);
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-helper-suggestions.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-helper-suggestions.js
index 57c1d393f3b..e2f665eb33c 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-helper-suggestions.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-helper-suggestions.js
@@ -4,7 +4,7 @@ import { i18n } from "discourse-i18n";
export const MIN_CHARACTER_COUNT = 40;
-function tagNames(tags) {
+export function tagNames(tags) {
return (tags ?? [])
.map((tag) => (typeof tag === "string" ? tag : tag?.name))
.filter(Boolean);
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/chooser-suggestion-context.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/chooser-suggestion-context.js
new file mode 100644
index 00000000000..64c005bfc22
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/chooser-suggestion-context.js
@@ -0,0 +1,108 @@
+import { get } from "@ember/object";
+import { getOwner } from "@ember/owner";
+import {
+ MIN_CHARACTER_COUNT,
+ tagNames,
+ tagSuggestionParams,
+} from "./ai-helper-suggestions";
+
+class ComposerSuggestionContext {
+ constructor(composer) {
+ this.composer = composer;
+ }
+
+ get model() {
+ return this.composer.model;
+ }
+
+ get categoryChooserEnabled() {
+ return !this.composer.disableCategoryChooser;
+ }
+
+ get tagChooserEnabled() {
+ return !this.composer.disableTagsChooser;
+ }
+
+ get selectedTagNames() {
+ return tagNames(this.composer.model?.tags);
+ }
+
+ get available() {
+ return (this.model?.reply?.length ?? 0) > MIN_CHARACTER_COUNT;
+ }
+
+ categoryRequestData() {
+ const text = this.model?.reply;
+ if (!text || text.length < MIN_CHARACTER_COUNT) {
+ return null;
+ }
+ return { text };
+ }
+
+ tagRequestData() {
+ const text = this.model?.reply;
+ if (!text || text.length < MIN_CHARACTER_COUNT) {
+ return null;
+ }
+ return {
+ text,
+ ...tagSuggestionParams(this.model.categoryId, this.model.tags),
+ };
+ }
+}
+
+class EditTopicSuggestionContext {
+ constructor(topicController) {
+ this.topicController = topicController;
+ }
+
+ get model() {
+ return { privateMessage: this.topicController.model?.isPrivateMessage };
+ }
+
+ get categoryChooserEnabled() {
+ return true;
+ }
+
+ get tagChooserEnabled() {
+ return true;
+ }
+
+ get selectedTagNames() {
+ const buffered = this.topicController.buffered;
+ return tagNames(buffered && get(buffered, "tags"));
+ }
+
+ get available() {
+ return true;
+ }
+
+ categoryRequestData() {
+ return { topic_id: this.topicController.model.id };
+ }
+
+ tagRequestData() {
+ return { topic_id: this.topicController.model.id };
+ }
+}
+
+export function chooserSuggestionContext(component) {
+ const owner = getOwner(component);
+ const element = component.element;
+
+ if (element?.closest("#reply-control")) {
+ const composer = owner.lookup("service:composer");
+ if (composer?.model) {
+ return new ComposerSuggestionContext(composer);
+ }
+ }
+
+ if (element?.closest(".edit-topic-title")) {
+ const topicController = owner.lookup("controller:topic");
+ if (topicController?.editingTopic && topicController.model) {
+ return new EditTopicSuggestionContext(topicController);
+ }
+ }
+
+ return null;
+}
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss
index 668f0bcad4e..5759a54f66e 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-helper/common/ai-helper.scss
@@ -251,41 +251,46 @@
}
}
+.edit-title__wrapper {
+ &:has(.ai-title-suggester-trigger) {
+ display: flex;
+ gap: var(--space-1);
+ }
+}
+
.edit-title__wrapper,
.edit-category__wrapper,
.edit-tags__wrapper {
position: relative;
}
-.suggest-titles-button,
-.suggest-tags-button,
-.suggest-category-button {
+.suggest-titles-button {
position: absolute;
- right: 0;
- top: 1px; // container border width
- z-index: z("dropdown");
- #reply-control & {
- z-index: z("composer", "dropdown") + 1;
- }
-}
-
-.suggest-titles-button {
- #reply-control & {
+ #reply-control &,
+ .edit-topic-title & {
background: var(--primary-low);
right: var(--space-1);
- top: 50%;
- transform: translateY(
- calc(-50% - 0.25em)
- ); // .25em = half of input margin-bottom padding
+ bottom: 50%;
border-radius: var(--d-input-border-radius); // to match surrounding input
padding: 0.4em;
font-size: var(--font-down-1-rem);
+ z-index: z("composer", "dropdown") + 1;
.d-icon {
color: var(--tertiary);
}
}
+
+ .edit-topic-title & {
+ transform: translateY(50%);
+ }
+
+ #reply-control & {
+ transform: translateY(
+ calc(50% - 0.25em)
+ ); // .25em = half of input margin-bottom padding
+ }
}
#reply-control {
@@ -308,50 +313,6 @@
text-align: left;
}
-.ai-category-suggester-content {
- .category-row {
- padding: 0.25em 0.5em;
- color: var(--primary-high);
-
- &:hover {
- background: var(--d-hover);
- }
- }
-
- .topic-count {
- font-size: var(--font-down-2);
- }
-}
-
-.ai-tag-suggester-content {
- .tag-row {
- .discourse-tag-count {
- margin-left: 5px;
- }
-
- .d-button-label {
- display: none;
- }
- }
-}
-
-.edit-topic-title {
- .suggestion-button {
- margin: 0;
- padding: 0.465rem;
- }
-}
-
-#topic-title .edit-topic-title.showing-ai-suggestions {
- #edit-title {
- flex: 1 1 90%;
- }
-
- .suggest-titles-button {
- padding: 0.5rem;
- }
-}
-
@keyframes spin {
0% {
transform: rotate(0deg);
@@ -675,9 +636,7 @@
.fk-d-menu {
&[data-identifier="ai-composer-helper-menu"],
- &[data-identifier="ai-title-suggester"],
- &[data-identifier="ai-category-suggester"],
- &[data-identifier="ai-tag-suggester"] {
+ &[data-identifier="ai-title-suggester"] {
z-index: z("modal", "dialog");
.fullscreen-composer & {
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 79d163fdfed..6a80c439193 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1083,9 +1083,6 @@ en:
choose: "Choose for me"
suggestions: "Suggestions"
suggest_errors:
- too_many_tags:
- one: "You can only have up to %{count} tag"
- other: "You can only have up to %{count} tags"
no_suggestions: "No suggestions available"
missing_content: "Please enter some content to generate suggestions."
context_menu:
diff --git a/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb b/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb
index 4a86d175fb3..7344baec8a8 100644
--- a/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb
@@ -303,43 +303,114 @@ RSpec.describe "AI Composer helper" do
end
end
- context "when suggesting the tags with AI tag suggester" do
+ context "when suggesting a category inline when editing a topic" do
+ let(:edit_category_chooser) do
+ PageObjects::Components::SelectKit.new(".edit-category__wrapper .category-chooser")
+ end
+
before do
SiteSetting.ai_embeddings_selected_model = embedding_definition.id
SiteSetting.ai_embeddings_enabled = true
+ stub_request(:post, embedding_definition.url).to_return(
+ status: 200,
+ body: JSON.dump([[0.0038493] * embedding_definition.dimensions]),
+ )
+ end
+
+ it "applies the best suggestion and closes the dropdown" do
+ response = [
+ {
+ id: category_2.id,
+ name: category_2.name,
+ slug: category_2.slug,
+ color: category_2.color,
+ topicCount: 1,
+ score: 1.0,
+ },
+ ]
+ DiscourseAi::AiHelper::SemanticCategorizer.any_instance.stubs(:categories).returns(response)
+
+ topic_page.visit_topic(topic)
+ page.find(".edit-topic", visible: false).click
+
+ edit_category_chooser.expand
+ edit_category_chooser.select_row_by_value("ai-category-suggest")
+
+ expect(edit_category_chooser).to have_selected_name(category_2.name)
+ expect(page).to have_no_css(".edit-category__wrapper .category-chooser.is-expanded")
end
- it "does not suggest tags that already exist" do
+ it "does not offer a suggestion when embeddings are disabled" do
+ SiteSetting.ai_embeddings_enabled = false
+
+ topic_page.visit_topic(topic)
+ page.find(".edit-topic", visible: false).click
+
+ edit_category_chooser.expand
+
+ expect(page).to have_no_css(
+ ".edit-category__wrapper .category-chooser .select-kit-row[data-value='ai-category-suggest']",
+ )
+ end
+ end
+
+ context "when suggesting tags inline when editing a topic" do
+ let(:edit_tag_chooser) do
+ PageObjects::Components::SelectKit.new(".edit-tags__wrapper .mini-tag-chooser")
+ end
+
+ before do
+ SiteSetting.ai_embeddings_selected_model = embedding_definition.id
+ SiteSetting.ai_embeddings_enabled = true
+ stub_request(:post, embedding_definition.url).to_return(
+ status: 200,
+ body: JSON.dump([[0.0038493] * embedding_definition.dimensions]),
+ )
+ end
+
+ it "does not suggest tags that are already on the topic" do
response =
- Tag
- .take(7)
- .map { |t| { id: t.id, name: t.name, score: rand(0.0...45.0) } }
- .sort_by { |h| h[:score] }
+ [cloud, feedback, review, video, music].map { |t| { id: t.id, name: t.name, count: 1 } }
DiscourseAi::AiHelper::SemanticCategorizer.any_instance.stubs(:tags).returns(response)
topic_page.visit_topic(topic)
page.find(".edit-topic", visible: false).click
- page.find(".ai-tag-suggester-trigger").click
- tag1_css = ".ai-suggestions-menu button[data-name='#{video.name}']"
- tag2_css = ".ai-suggestions-menu button[data-name='#{music.name}']"
- expect(page).to have_no_css(tag1_css)
- expect(page).to have_no_css(tag2_css)
+ edit_tag_chooser.expand
+ edit_tag_chooser.select_row_by_value("ai-tag-suggest")
+
+ expect(edit_tag_chooser).to have_option_name(cloud.name)
+ expect(edit_tag_chooser).to have_no_option_name(video.name)
+ expect(edit_tag_chooser).to have_no_option_name(music.name)
end
- it "shows filtered suggestions when topic already has tags" do
- response =
- [cloud, feedback, review, video, music].map { |t| { id: t.id, name: t.name, score: 1.0 } }
+ it "applies a suggested tag and stops offering it once added" do
+ response = [cloud, feedback].map { |t| { id: t.id, name: t.name, count: 1 } }
DiscourseAi::AiHelper::SemanticCategorizer.any_instance.stubs(:tags).returns(response)
topic_page.visit_topic(topic)
page.find(".edit-topic", visible: false).click
- page.find(".ai-tag-suggester-trigger").click
- wait_for { ai_suggestion_dropdown.has_dropdown? }
+ edit_tag_chooser.expand
+ edit_tag_chooser.select_row_by_value("ai-tag-suggest")
+ edit_tag_chooser.select_row_by_name(cloud.name)
+
+ expect(edit_tag_chooser).to have_selected_choice_name(cloud.name)
+ expect(edit_tag_chooser).to have_no_option_name(cloud.name)
+ expect(edit_tag_chooser).to have_option_name(feedback.name)
+ end
+
+ it "does not offer a suggestion when embeddings are disabled" do
+ SiteSetting.ai_embeddings_enabled = false
+
+ topic_page.visit_topic(topic)
+ page.find(".edit-topic", visible: false).click
+
+ edit_tag_chooser.expand
- expect(page).to have_no_css(".ai-suggestions-menu button[data-name='#{video.name}']")
- expect(page).to have_no_css(".ai-suggestions-menu button[data-name='#{music.name}']")
+ expect(page).to have_no_css(
+ ".edit-tags__wrapper .mini-tag-chooser .select-kit-row[data-value='ai-tag-suggest']",
+ )
end
end
diff --git a/plugins/discourse-ai/spec/system/page_objects/components/ai_suggestion_dropdown.rb b/plugins/discourse-ai/spec/system/page_objects/components/ai_suggestion_dropdown.rb
index 5d061dad8e0..eed37efbb97 100644
--- a/plugins/discourse-ai/spec/system/page_objects/components/ai_suggestion_dropdown.rb
+++ b/plugins/discourse-ai/spec/system/page_objects/components/ai_suggestion_dropdown.rb
@@ -5,22 +5,12 @@ module PageObjects
class AiSuggestionDropdown < PageObjects::Components::Base
SUGGESTION_BUTTON_SELECTOR = ".suggestion-button"
TITLE_BUTTON_SELECTOR = "#{SUGGESTION_BUTTON_SELECTOR}.suggest-titles-button"
- CATEGORY_BUTTON_SELECTOR = "#{SUGGESTION_BUTTON_SELECTOR}.suggest-category-button"
- TAG_BUTTON_SELECTOR = "#{SUGGESTION_BUTTON_SELECTOR}.suggest-tags-button"
MENU_SELECTOR = ".ai-suggestions-menu"
def click_suggest_titles_button
page.find(TITLE_BUTTON_SELECTOR, visible: :all).click
end
- def click_suggest_category_button
- find(CATEGORY_BUTTON_SELECTOR, visible: :all).click
- end
-
- def click_suggest_tags_button
- find(TAG_BUTTON_SELECTOR, visible: :all).click
- end
-
def select_suggestion_by_value(index)
find("#{MENU_SELECTOR} button[data-value=\"#{index}\"]").click
end