FIX: Conditionally render AI suggestion buttons (#225)

This commit is contained in:
Keegan George 2023-09-13 14:18:48 -07:00 committed by GitHub
parent cf6a6ed9b8
commit d35c8d5eca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 18 deletions

View File

@ -1,6 +1,8 @@
import Component from '@glimmer/component';
import AISuggestionDropdown from "../../components/ai-suggestion-dropdown";
import { inject as service } from "@ember/service";
import showAIHelper from "../../lib/show-ai-helper";
export default class AICategorySuggestion extends Component {
<template>
@ -9,5 +11,9 @@ export default class AICategorySuggestion extends Component {
{{/if}}
</template>
static shouldRender(outletArgs, helper) {
return showAIHelper(outletArgs, helper);
}
@service siteSettings;
}

View File

@ -1,6 +1,7 @@
import Component from '@glimmer/component';
import AISuggestionDropdown from "../../components/ai-suggestion-dropdown";
import { inject as service } from "@ember/service";
import showAIHelper from "../../lib/show-ai-helper";
export default class AITagSuggestion extends Component {
@ -10,5 +11,9 @@ export default class AITagSuggestion extends Component {
{{/if}}
</template>
static shouldRender(outletArgs, helper) {
return showAIHelper(outletArgs, helper);
}
@service siteSettings;
}

View File

@ -1,8 +1,13 @@
import Component from '@glimmer/component';
import AISuggestionDropdown from "../../components/ai-suggestion-dropdown";
import showAIHelper from "../../lib/show-ai-helper";
export default class AITitleSuggestion extends Component {
<template>
<AISuggestionDropdown @mode="suggest_title" @composer={{@outletArgs.composer}} class="suggest-titles-button" />
</template>
static shouldRender(outletArgs, helper) {
return showAIHelper(outletArgs, helper);
}
}

View File

@ -8,27 +8,11 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
import { createPopper } from "@popperjs/core";
import { caretPosition, getCaretPosition } from "discourse/lib/utilities";
import { inject as service } from "@ember/service";
import showAIHelper from "../../lib/show-ai-helper";
export default class AiHelperContextMenu extends Component {
static shouldRender(outletArgs, helper) {
const helperEnabled =
helper.siteSettings.discourse_ai_enabled &&
helper.siteSettings.composer_ai_helper_enabled;
const allowedGroups = helper.siteSettings.ai_helper_allowed_groups
.split("|")
.map((id) => parseInt(id, 10));
const canUseAssistant = helper.currentUser?.groups.some((g) =>
allowedGroups.includes(g.id)
);
const canShowInPM = helper.siteSettings.ai_helper_allowed_in_pm;
if (outletArgs?.composer?.privateMessage) {
return helperEnabled && canUseAssistant && canShowInPM;
}
return helperEnabled && canUseAssistant;
return showAIHelper(outletArgs, helper);
}
@service siteSettings;

View File

@ -0,0 +1,20 @@
export default function showAIHelper(outletArgs, helper) {
const helperEnabled =
helper.siteSettings.discourse_ai_enabled &&
helper.siteSettings.composer_ai_helper_enabled;
const allowedGroups = helper.siteSettings.ai_helper_allowed_groups
.split("|")
.map((id) => parseInt(id, 10));
const canUseAssistant = helper.currentUser?.groups.some((g) =>
allowedGroups.includes(g.id)
);
const canShowInPM = helper.siteSettings.ai_helper_allowed_in_pm;
if (outletArgs?.composer?.privateMessage) {
return helperEnabled && canUseAssistant && canShowInPM;
}
return helperEnabled && canUseAssistant;
}

View File

@ -4,6 +4,7 @@ require_relative "../../support/openai_completions_inference_stubs"
RSpec.describe "AI Composer helper", type: :system, js: true do
fab!(:user) { Fabricate(:admin) }
fab!(:non_member_group) { Fabricate(:group) }
before do
Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user)
@ -330,4 +331,44 @@ RSpec.describe "AI Composer helper", type: :system, js: true do
expect(tag_selector["data-name"]).to eq(suggestion)
end
end
context "when AI helper is disabled" do
let(:mode) { OpenAiCompletionsInferenceStubs::GENERATE_TITLES }
before do
OpenAiCompletionsInferenceStubs.stub_prompt(mode)
SiteSetting.composer_ai_helper_enabled = false
end
it "does not trigger AI context menu" do
trigger_context_menu(OpenAiCompletionsInferenceStubs.translated_response)
expect(ai_helper_context_menu).to have_no_context_menu
end
it "does not trigger AI suggestion buttons" do
visit("/latest")
page.find("#create-topic").click
composer.fill_content(OpenAiCompletionsInferenceStubs.translated_response)
expect(ai_suggestion_dropdown).to have_no_suggestion_button
end
end
context "when user is not a member of AI helper allowed group" do
let(:mode) { OpenAiCompletionsInferenceStubs::GENERATE_TITLES }
before do
OpenAiCompletionsInferenceStubs.stub_prompt(mode)
SiteSetting.ai_helper_allowed_groups = non_member_group.id.to_s
end
it "does not trigger AI context menu" do
trigger_context_menu(OpenAiCompletionsInferenceStubs.translated_response)
expect(ai_helper_context_menu).to have_no_context_menu
end
it "does not trigger AI suggestion buttons" do
visit("/latest")
page.find("#create-topic").click
composer.fill_content(OpenAiCompletionsInferenceStubs.translated_response)
expect(ai_suggestion_dropdown).to have_no_suggestion_button
end
end
end