2023-11-03 07:30:09 -04:00
|
|
|
import Component from "@glimmer/component";
|
2023-10-23 10:41:36 -04:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2024-01-12 18:28:06 -05:00
|
|
|
import { fn } from "@ember/helper";
|
2023-10-23 10:41:36 -04:00
|
|
|
import { action } from "@ember/object";
|
2023-12-12 12:28:39 -05:00
|
|
|
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
|
|
|
|
import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
|
|
|
|
import { inject as service } from "@ember/service";
|
2024-01-10 13:41:48 -05:00
|
|
|
import CookText from "discourse/components/cook-text";
|
2023-11-03 07:30:09 -04:00
|
|
|
import DButton from "discourse/components/d-button";
|
2023-12-14 22:30:52 -05:00
|
|
|
import FastEdit from "discourse/components/fast-edit";
|
|
|
|
import FastEditModal from "discourse/components/modal/fast-edit";
|
2023-10-23 10:41:36 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-01-10 13:41:48 -05:00
|
|
|
import { clipboardCopy } from "discourse/lib/utilities";
|
2023-12-12 12:28:39 -05:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
2023-11-03 07:30:09 -04:00
|
|
|
import eq from "truth-helpers/helpers/eq";
|
2023-11-17 12:25:41 -05:00
|
|
|
import not from "truth-helpers/helpers/not";
|
2023-12-14 11:47:20 -05:00
|
|
|
import AiHelperCustomPrompt from "../../components/ai-helper-custom-prompt";
|
2023-12-12 12:28:39 -05:00
|
|
|
import AiHelperLoading from "../../components/ai-helper-loading";
|
2023-11-29 17:01:48 -05:00
|
|
|
import { showPostAIHelper } from "../../lib/show-ai-helper";
|
2023-10-23 10:41:36 -04:00
|
|
|
|
|
|
|
export default class AIHelperOptionsMenu extends Component {
|
|
|
|
static shouldRender(outletArgs, helper) {
|
|
|
|
return showPostAIHelper(outletArgs, helper);
|
|
|
|
}
|
2024-01-12 18:28:06 -05:00
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
@service messageBus;
|
2023-12-14 22:30:52 -05:00
|
|
|
@service site;
|
|
|
|
@service modal;
|
2023-12-14 11:47:20 -05:00
|
|
|
@service siteSettings;
|
|
|
|
@service currentUser;
|
2023-12-22 14:53:41 -05:00
|
|
|
@service menu;
|
2024-01-12 18:28:06 -05:00
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
@tracked menuState = this.MENU_STATES.triggers;
|
|
|
|
@tracked loading = false;
|
|
|
|
@tracked suggestion = "";
|
|
|
|
@tracked showMainButtons = true;
|
2023-12-14 11:47:20 -05:00
|
|
|
@tracked customPromptValue = "";
|
2023-11-17 12:25:41 -05:00
|
|
|
@tracked copyButtonIcon = "copy";
|
|
|
|
@tracked copyButtonLabel = "discourse_ai.ai_helper.post_options_menu.copy";
|
2023-12-14 22:30:52 -05:00
|
|
|
@tracked showFastEdit = false;
|
|
|
|
@tracked showAiButtons = true;
|
2023-11-17 12:25:41 -05:00
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
MENU_STATES = {
|
|
|
|
triggers: "TRIGGERS",
|
|
|
|
options: "OPTIONS",
|
|
|
|
loading: "LOADING",
|
2023-11-03 07:30:09 -04:00
|
|
|
result: "RESULT",
|
2023-10-23 10:41:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
@tracked _activeAIRequest = null;
|
|
|
|
|
|
|
|
@action
|
|
|
|
async showAIHelperOptions() {
|
|
|
|
this.showMainButtons = false;
|
|
|
|
this.menuState = this.MENU_STATES.options;
|
2023-12-22 14:53:41 -05:00
|
|
|
this.menu.activeMenu.options.placement = "bottom";
|
2023-10-23 10:41:36 -04:00
|
|
|
}
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
@bind
|
|
|
|
subscribe() {
|
|
|
|
const channel = `/discourse-ai/ai-helper/explain/${this.args.outletArgs.data.quoteState.postId}`;
|
|
|
|
this.messageBus.subscribe(channel, this._updateResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
unsubscribe() {
|
|
|
|
this.messageBus.unsubscribe(
|
|
|
|
"/discourse-ai/ai-helper/explain/*",
|
|
|
|
this._updateResult
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
_updateResult(result) {
|
2024-01-10 13:41:48 -05:00
|
|
|
this.suggestion = result.result;
|
2023-12-12 12:28:39 -05:00
|
|
|
}
|
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
@action
|
|
|
|
async performAISuggestion(option) {
|
|
|
|
this.menuState = this.MENU_STATES.loading;
|
|
|
|
|
2023-12-14 22:30:52 -05:00
|
|
|
if (option.name === "explain") {
|
2023-12-12 12:28:39 -05:00
|
|
|
this.menuState = this.MENU_STATES.result;
|
|
|
|
|
|
|
|
const fetchUrl = `/discourse-ai/ai-helper/explain`;
|
|
|
|
this._activeAIRequest = ajax(fetchUrl, {
|
2023-10-23 10:41:36 -04:00
|
|
|
method: "POST",
|
|
|
|
data: {
|
|
|
|
mode: option.value,
|
|
|
|
text: this.args.outletArgs.data.quoteState.buffer,
|
2023-11-03 07:30:09 -04:00
|
|
|
post_id: this.args.outletArgs.data.quoteState.postId,
|
|
|
|
},
|
2023-10-23 10:41:36 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._activeAIRequest = ajax("/discourse-ai/ai-helper/suggest", {
|
|
|
|
method: "POST",
|
|
|
|
data: {
|
2023-12-14 11:47:20 -05:00
|
|
|
mode: option.id,
|
2023-10-23 10:41:36 -04:00
|
|
|
text: this.args.outletArgs.data.quoteState.buffer,
|
2023-12-14 11:47:20 -05:00
|
|
|
custom_prompt: this.customPromptValue,
|
2023-11-03 07:30:09 -04:00
|
|
|
},
|
2023-10-23 10:41:36 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:30:52 -05:00
|
|
|
if (option.name !== "explain") {
|
2023-12-14 11:47:20 -05:00
|
|
|
this._activeAIRequest
|
|
|
|
.then(({ suggestions }) => {
|
2023-12-14 22:30:52 -05:00
|
|
|
this.suggestion = suggestions[0].trim();
|
|
|
|
|
|
|
|
if (option.name === "proofread") {
|
|
|
|
this.showAiButtons = false;
|
|
|
|
|
|
|
|
if (this.site.desktopView) {
|
|
|
|
this.showFastEdit = true;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
return this.modal.show(FastEditModal, {
|
|
|
|
model: {
|
|
|
|
initialValue: this.args.outletArgs.data.quoteState.buffer,
|
|
|
|
newValue: this.suggestion,
|
|
|
|
post: this.args.outletArgs.post,
|
|
|
|
close: this.closeFastEdit,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-12-14 11:47:20 -05:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this.loading = false;
|
|
|
|
this.menuState = this.MENU_STATES.result;
|
|
|
|
});
|
2023-12-12 12:28:39 -05:00
|
|
|
}
|
2023-10-23 10:41:36 -04:00
|
|
|
|
|
|
|
return this._activeAIRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelAIAction() {
|
|
|
|
if (this._activeAIRequest) {
|
|
|
|
this._activeAIRequest.abort();
|
|
|
|
this._activeAIRequest = null;
|
|
|
|
this.loading = false;
|
|
|
|
this.menuState = this.MENU_STATES.options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 12:25:41 -05:00
|
|
|
@action
|
|
|
|
copySuggestion() {
|
|
|
|
if (this.suggestion?.length > 0) {
|
2024-01-10 13:41:48 -05:00
|
|
|
clipboardCopy(this.suggestion);
|
|
|
|
this.copyButtonIcon = "check";
|
|
|
|
this.copyButtonLabel = "discourse_ai.ai_helper.post_options_menu.copied";
|
|
|
|
setTimeout(() => {
|
|
|
|
this.copyButtonIcon = "copy";
|
|
|
|
this.copyButtonLabel = "discourse_ai.ai_helper.post_options_menu.copy";
|
|
|
|
}, 3500);
|
2023-11-17 12:25:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:57:14 -05:00
|
|
|
get helperOptions() {
|
|
|
|
let prompts = this.currentUser?.ai_helper_prompts;
|
2023-10-23 10:41:36 -04:00
|
|
|
|
2023-12-14 11:47:20 -05:00
|
|
|
prompts = prompts.filter((item) => item.location.includes("post"));
|
|
|
|
|
|
|
|
// Find the custom_prompt object and move it to the beginning of the array
|
|
|
|
const customPromptIndex = prompts.findIndex(
|
|
|
|
(p) => p.name === "custom_prompt"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (customPromptIndex !== -1) {
|
|
|
|
const customPrompt = prompts.splice(customPromptIndex, 1)[0];
|
|
|
|
prompts.unshift(customPrompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._showUserCustomPrompts()) {
|
|
|
|
prompts = prompts.filter((p) => p.name !== "custom_prompt");
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:30:52 -05:00
|
|
|
if (!this.args.outletArgs.data.canEditPost) {
|
|
|
|
prompts = prompts.filter((p) => p.name !== "proofread");
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:57:14 -05:00
|
|
|
return prompts;
|
2023-12-14 11:47:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_showUserCustomPrompts() {
|
|
|
|
const allowedGroups =
|
|
|
|
this.siteSettings?.ai_helper_custom_prompts_allowed_groups
|
|
|
|
.split("|")
|
|
|
|
.map((id) => parseInt(id, 10));
|
|
|
|
|
|
|
|
return this.currentUser?.groups.some((g) => allowedGroups.includes(g.id));
|
2023-10-23 10:41:36 -04:00
|
|
|
}
|
2023-11-03 07:30:09 -04:00
|
|
|
|
2023-12-14 22:30:52 -05:00
|
|
|
@action
|
|
|
|
async closeFastEdit() {
|
|
|
|
this.showFastEdit = false;
|
|
|
|
await this.args.outletArgs.data.hideToolbar();
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:30:09 -04:00
|
|
|
<template>
|
|
|
|
{{#if this.showMainButtons}}
|
|
|
|
{{yield}}
|
|
|
|
{{/if}}
|
|
|
|
|
2023-12-14 22:30:52 -05:00
|
|
|
{{#if this.showAiButtons}}
|
|
|
|
<div class="ai-post-helper">
|
|
|
|
{{#if (eq this.menuState this.MENU_STATES.triggers)}}
|
|
|
|
<DButton
|
|
|
|
@icon="discourse-sparkles"
|
|
|
|
@title="discourse_ai.ai_helper.post_options_menu.title"
|
|
|
|
@label="discourse_ai.ai_helper.post_options_menu.trigger"
|
|
|
|
@action={{this.showAIHelperOptions}}
|
2024-01-12 18:28:06 -05:00
|
|
|
class="btn-flat ai-post-helper__trigger"
|
2023-12-14 22:30:52 -05:00
|
|
|
/>
|
|
|
|
|
|
|
|
{{else if (eq this.menuState this.MENU_STATES.options)}}
|
|
|
|
<div class="ai-post-helper__options">
|
|
|
|
{{#each this.helperOptions as |option|}}
|
|
|
|
{{#if (eq option.name "custom_prompt")}}
|
|
|
|
<AiHelperCustomPrompt
|
|
|
|
@value={{this.customPromptValue}}
|
|
|
|
@promptArgs={{option}}
|
|
|
|
@submit={{this.performAISuggestion}}
|
|
|
|
/>
|
|
|
|
{{else}}
|
|
|
|
<DButton
|
|
|
|
@icon={{option.icon}}
|
|
|
|
@translatedLabel={{option.translated_name}}
|
2024-01-12 18:28:06 -05:00
|
|
|
@action={{fn this.performAISuggestion option}}
|
2023-12-14 22:30:52 -05:00
|
|
|
data-name={{option.name}}
|
|
|
|
data-value={{option.id}}
|
2024-01-12 18:28:06 -05:00
|
|
|
class="btn-flat ai-post-helper__options-button"
|
2023-12-14 22:30:52 -05:00
|
|
|
/>
|
|
|
|
{{/if}}
|
|
|
|
{{/each}}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{{else if (eq this.menuState this.MENU_STATES.loading)}}
|
|
|
|
<AiHelperLoading @cancel={{this.cancelAIAction}} />
|
|
|
|
{{else if (eq this.menuState this.MENU_STATES.result)}}
|
|
|
|
<div
|
|
|
|
class="ai-post-helper__suggestion"
|
|
|
|
{{didInsert this.subscribe}}
|
|
|
|
{{willDestroy this.unsubscribe}}
|
|
|
|
>
|
|
|
|
{{#if this.suggestion}}
|
2023-12-15 11:51:47 -05:00
|
|
|
<div class="ai-post-helper__suggestion__text" dir="auto">
|
2024-01-10 13:41:48 -05:00
|
|
|
<CookText @rawText={{this.suggestion}} />
|
2023-12-14 22:30:52 -05:00
|
|
|
</div>
|
|
|
|
<di class="ai-post-helper__suggestion__buttons">
|
|
|
|
<DButton
|
|
|
|
@icon="times"
|
|
|
|
@label="discourse_ai.ai_helper.post_options_menu.cancel"
|
|
|
|
@action={{this.cancelAIAction}}
|
2024-01-12 18:28:06 -05:00
|
|
|
class="btn-flat ai-post-helper__suggestion__cancel"
|
2023-12-14 22:30:52 -05:00
|
|
|
/>
|
|
|
|
<DButton
|
|
|
|
@icon={{this.copyButtonIcon}}
|
|
|
|
@label={{this.copyButtonLabel}}
|
|
|
|
@action={{this.copySuggestion}}
|
|
|
|
@disabled={{not this.suggestion}}
|
2024-01-12 18:28:06 -05:00
|
|
|
class="btn-flat ai-post-helper__suggestion__copy"
|
2023-12-14 22:30:52 -05:00
|
|
|
/>
|
|
|
|
</di>
|
2023-12-14 11:47:20 -05:00
|
|
|
{{else}}
|
2023-12-14 22:30:52 -05:00
|
|
|
<AiHelperLoading @cancel={{this.cancelAIAction}} />
|
2023-12-14 11:47:20 -05:00
|
|
|
{{/if}}
|
2023-12-14 22:30:52 -05:00
|
|
|
</div>
|
|
|
|
{{/if}}
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
|
|
|
|
|
|
|
{{#if this.showFastEdit}}
|
|
|
|
<div class="ai-post-helper__fast-edit">
|
|
|
|
<FastEdit
|
|
|
|
@initialValue={{@outletArgs.data.quoteState.buffer}}
|
|
|
|
@newValue={{this.suggestion}}
|
|
|
|
@post={{@outletArgs.post}}
|
|
|
|
@close={{this.closeFastEdit}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{{/if}}
|
2023-11-03 07:30:09 -04:00
|
|
|
</template>
|
2023-11-29 17:01:48 -05:00
|
|
|
}
|