mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-08-03 19:53:28 +00:00
You can now edit each AI helper prompt individually through personas, limit access to specific groups, set different LLMs, etc.
91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
import { i18n } from "discourse-i18n";
|
|
import AiComposerHelperMenu from "../discourse/components/ai-composer-helper-menu";
|
|
import ModalDiffModal from "../discourse/components/modal/diff-modal";
|
|
import { showComposerAiHelper } from "../discourse/lib/show-ai-helper";
|
|
|
|
function initializeAiHelperTrigger(api) {
|
|
api.onToolbarCreate((toolbar) => {
|
|
const currentUser = api.getCurrentUser();
|
|
const modal = api.container.lookup("service:modal");
|
|
|
|
const selectedText = (toolbarEvent) => {
|
|
const composerContent = toolbarEvent.getText();
|
|
const selected = toolbarEvent.selected.value;
|
|
|
|
if (selected && selected.length > 0) {
|
|
return selected;
|
|
}
|
|
|
|
if (composerContent && composerContent.length > 0) {
|
|
return composerContent;
|
|
}
|
|
};
|
|
toolbar.addButton({
|
|
id: "ai-helper-trigger",
|
|
group: "extras",
|
|
icon: "discourse-sparkles",
|
|
title: "discourse_ai.ai_helper.context_menu.trigger",
|
|
preventFocus: true,
|
|
hideShortcutInTitle: true,
|
|
shortcut: "ALT+P",
|
|
shortcutAction: (toolbarEvent) => {
|
|
if (toolbarEvent.getText().length === 0) {
|
|
const toasts = api.container.lookup("service:toasts");
|
|
|
|
return toasts.error({
|
|
class: "ai-proofread-error-toast",
|
|
duration: 3000,
|
|
data: {
|
|
message: i18n("discourse_ai.ai_helper.no_content_error"),
|
|
},
|
|
});
|
|
}
|
|
|
|
const mode = currentUser?.ai_helper_prompts.find(
|
|
(p) => p.name === "proofread"
|
|
).name;
|
|
|
|
modal.show(ModalDiffModal, {
|
|
model: {
|
|
mode,
|
|
selectedText: selectedText(toolbarEvent),
|
|
toolbarEvent,
|
|
showResultAsDiff: true,
|
|
},
|
|
});
|
|
},
|
|
condition: () =>
|
|
showComposerAiHelper(
|
|
api.container.lookup("service:composer").model,
|
|
api.container.lookup("service:site-settings"),
|
|
currentUser,
|
|
"context_menu"
|
|
),
|
|
sendAction: (event) => {
|
|
const menu = api.container.lookup("service:menu");
|
|
menu.show(document.querySelector(".ai-helper-trigger"), {
|
|
identifier: "ai-composer-helper-menu",
|
|
component: AiComposerHelperMenu,
|
|
modalForMobile: true,
|
|
interactive: true,
|
|
data: {
|
|
toolbarEvent: event,
|
|
selectedText: selectedText(event),
|
|
},
|
|
});
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|
|
export default {
|
|
name: "discourse-ai-helper",
|
|
|
|
initialize() {
|
|
withPluginApi("1.1.0", (api) => {
|
|
initializeAiHelperTrigger(api);
|
|
});
|
|
},
|
|
};
|