mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-14 23:44:42 +00:00
* FEATURE: Composer AI helper This change introduces a new composer button for the group members listed in the `ai_helper_allowed_groups` site setting. Users can use chatGPT to review, improve, or translate their posts to English. * Add a safeguard for PMs and don't rely on parentView
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
function initializeComposerAIHelper(api) {
|
|
api.modifyClass("component:composer-editor", {
|
|
actions: {
|
|
extraButtons(toolbar) {
|
|
this._super(toolbar);
|
|
|
|
const removeAiHelperFromPM =
|
|
this.composerModel.privateMessage &&
|
|
!this.siteSettings.ai_helper_allowed_in_pm;
|
|
|
|
if (removeAiHelperFromPM) {
|
|
const extrasGroup = toolbar.groups.find((g) => g.group === "extras");
|
|
const newButtons = extrasGroup.buttons.filter(
|
|
(b) => b.id !== "ai-helper"
|
|
);
|
|
|
|
extrasGroup.buttons = newButtons;
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
api.modifyClass("component:d-editor", {
|
|
pluginId: "discourse-ai",
|
|
|
|
actions: {
|
|
openAIHelper() {
|
|
if (this.value) {
|
|
showModal("composer-ai-helper").setProperties({ editor: this });
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
api.onToolbarCreate((toolbar) => {
|
|
toolbar.addButton({
|
|
id: "ai-helper",
|
|
title: "discourse_ai.ai_helper.title",
|
|
group: "extras",
|
|
icon: "magic",
|
|
className: "composer-ai-helper",
|
|
sendAction: () => toolbar.context.send("openAIHelper"),
|
|
});
|
|
});
|
|
}
|
|
|
|
export default {
|
|
name: "discourse_ai-composer-helper",
|
|
|
|
initialize(container) {
|
|
const settings = container.lookup("site-settings:main");
|
|
const user = container.lookup("service:current-user");
|
|
|
|
const helperEnabled =
|
|
settings.discourse_ai_enabled && settings.composer_ai_helper_enabled;
|
|
|
|
const allowedGroups = settings.ai_helper_allowed_groups
|
|
.split("|")
|
|
.map(parseInt);
|
|
const canUseAssistant =
|
|
user && user.groups.some((g) => allowedGroups.includes(g.id));
|
|
|
|
if (helperEnabled && canUseAssistant) {
|
|
withPluginApi("1.6.0", initializeComposerAIHelper);
|
|
}
|
|
},
|
|
};
|