2023-12-14 22:30:52 -05:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { action } from "@ember/object";
|
2024-02-16 13:57:14 -05:00
|
|
|
import { inject as service } from "@ember/service";
|
2023-12-14 22:30:52 -05:00
|
|
|
import DButton from "discourse/components/d-button";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { showPostAIHelper } from "../../lib/show-ai-helper";
|
|
|
|
|
|
|
|
export default class AiEditSuggestionButton extends Component {
|
|
|
|
static shouldRender(outletArgs, helper) {
|
|
|
|
return showPostAIHelper(outletArgs, helper);
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:57:14 -05:00
|
|
|
@service currentUser;
|
2023-12-14 22:30:52 -05:00
|
|
|
@tracked loading = false;
|
|
|
|
@tracked suggestion = "";
|
|
|
|
@tracked _activeAIRequest = null;
|
|
|
|
|
|
|
|
get disabled() {
|
2023-12-15 18:19:04 -05:00
|
|
|
return this.loading || this.suggestion?.length > 0;
|
2023-12-14 22:30:52 -05:00
|
|
|
}
|
|
|
|
|
2024-02-16 13:57:14 -05:00
|
|
|
get mode() {
|
|
|
|
return this.currentUser?.ai_helper_prompts.find(
|
|
|
|
(prompt) => prompt.name === "proofread"
|
|
|
|
);
|
2023-12-14 22:30:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
suggest() {
|
|
|
|
this.loading = true;
|
|
|
|
this._activeAIRequest = ajax("/discourse-ai/ai-helper/suggest", {
|
|
|
|
method: "POST",
|
|
|
|
data: {
|
|
|
|
mode: this.mode.id,
|
|
|
|
text: this.args.outletArgs.initialValue,
|
|
|
|
custom_prompt: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this._activeAIRequest
|
|
|
|
.then(({ suggestions }) => {
|
|
|
|
this.suggestion = suggestions[0].trim();
|
|
|
|
this.args.outletArgs.updateValue(this.suggestion);
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this._activeAIRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
<template>
|
2023-12-15 18:19:04 -05:00
|
|
|
{{#unless @outletArgs.newValue}}
|
|
|
|
<DButton
|
|
|
|
class="btn-small btn-ai-suggest-edit"
|
|
|
|
@action={{this.suggest}}
|
|
|
|
@icon="discourse-sparkles"
|
|
|
|
@label="discourse_ai.ai_helper.fast_edit.suggest_button"
|
|
|
|
@isLoading={{this.loading}}
|
|
|
|
@disabled={{this.disabled}}
|
|
|
|
/>
|
|
|
|
{{/unless}}
|
2023-12-14 22:30:52 -05:00
|
|
|
</template>
|
|
|
|
}
|