FIX: Prevent proofreading when there is no content (#779)

This commit is contained in:
Keegan George 2024-08-28 12:21:34 -07:00 committed by GitHub
parent a08d168740
commit 943504049c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 1 deletions

View File

@ -1,10 +1,23 @@
import { withPluginApi } from "discourse/lib/plugin-api"; import { withPluginApi } from "discourse/lib/plugin-api";
import i18n from "discourse-common/helpers/i18n";
import ModalDiffModal from "../discourse/components/modal/diff-modal"; import ModalDiffModal from "../discourse/components/modal/diff-modal";
function initializeProofread(api) { function initializeProofread(api) {
api.addComposerToolbarPopupMenuOption({ api.addComposerToolbarPopupMenuOption({
action: (toolbarEvent) => { action: (toolbarEvent) => {
const modal = api.container.lookup("service:modal"); const modal = api.container.lookup("service:modal");
const composer = api.container.lookup("service:composer");
const toasts = api.container.lookup("service:toasts");
if (composer.model.reply?.length === 0) {
toasts.error({
duration: 3000,
data: {
message: i18n("discourse_ai.ai_helper.proofread.no_content_error"),
},
});
return;
}
modal.show(ModalDiffModal, { modal.show(ModalDiffModal, {
model: { model: {

View File

@ -241,7 +241,6 @@ en:
one: "This model is currently used by the %{settings} setting. If misconfigured, the feature won't work as expected." one: "This model is currently used by the %{settings} setting. If misconfigured, the feature won't work as expected."
other: "This model is currently used by the following settings: %{settings}. If misconfigured, features won't work as expected. " other: "This model is currently used by the following settings: %{settings}. If misconfigured, features won't work as expected. "
preconfigured_llms: "Select your LLM" preconfigured_llms: "Select your LLM"
preconfigured: preconfigured:
none: "Configure manually..." none: "Configure manually..."
@ -331,6 +330,8 @@ en:
prompt: "This post contains non-captioned images. Would you like to enable automatic AI captions on image uploads? (This can be changed in your preferences later)" prompt: "This post contains non-captioned images. Would you like to enable automatic AI captions on image uploads? (This can be changed in your preferences later)"
confirm: "Enable" confirm: "Enable"
cancel: "Don't ask again" cancel: "Don't ask again"
proofread:
no_content_error: "Unable to proofread text, please add some content to proofread."
reviewables: reviewables:
model_used: "Model used:" model_used: "Model used:"

View File

@ -10,6 +10,7 @@ RSpec.describe "AI Composer Proofreading Features", type: :system, js: true do
end end
let(:composer) { PageObjects::Components::Composer.new } let(:composer) { PageObjects::Components::Composer.new }
let(:toasts) { PageObjects::Components::Toasts.new }
it "proofreads selected text using the composer toolbar" do it "proofreads selected text using the composer toolbar" do
visit "/new-topic" visit "/new-topic"
@ -41,4 +42,16 @@ RSpec.describe "AI Composer Proofreading Features", type: :system, js: true do
expect(composer.composer_input.value).to eq("hello world") expect(composer.composer_input.value).to eq("hello world")
end end
end end
it "does not trigger proofread modal if composer is empty" do
visit "/new-topic"
# Simulate AI response
DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do
ai_toolbar = PageObjects::Components::SelectKit.new(".toolbar-popup-menu-options")
ai_toolbar.expand
ai_toolbar.select_row_by_name("Proofread Text")
expect(toasts).to have_error(I18n.t("js.discourse_ai.ai_helper.proofread.no_content_error"))
end
end
end end