FIX: Show illustrate post only if stability API key present (#395)

This commit is contained in:
Keegan George 2024-01-02 11:24:16 -08:00 committed by GitHub
parent f8fdb6db76
commit 1a5985134a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 1 deletions

View File

@ -5,8 +5,16 @@ module DiscourseAi
class Assistant class Assistant
def available_prompts(name_filter: nil) def available_prompts(name_filter: nil)
cp = CompletionPrompt cp = CompletionPrompt
prompts = []
prompts = name_filter ? [cp.enabled_by_name(name_filter)] : cp.where(enabled: true) if name_filter
prompts = [cp.enabled_by_name(name_filter)]
else
prompts = cp.where(enabled: true)
# Only show the illustrate_post prompt if the API key is present
prompts =
prompts.where.not(name: "illustrate_post") if !SiteSetting.ai_stability_api_key.present?
end
prompts.map do |prompt| prompts.map do |prompt|
translation = translation =

View File

@ -10,6 +10,52 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
defends himself, but instead exclaims: 'You too, my son!' Shakespeare and Quevedo capture the pathetic cry. defends himself, but instead exclaims: 'You too, my son!' Shakespeare and Quevedo capture the pathetic cry.
STRING STRING
describe("#available_prompts") do
context "when no name filter is provided" do
it "returns all available prompts" do
prompts = subject.available_prompts
expect(prompts.length).to eq(6)
expect(prompts.map { |p| p[:name] }).to contain_exactly(
"translate",
"generate_titles",
"proofread",
"markdown_table",
"custom_prompt",
"explain",
)
end
end
context "when name filter is provided" do
it "returns the prompt with the given name" do
prompts = subject.available_prompts(name_filter: "translate")
expect(prompts.length).to eq(1)
expect(prompts.first[:name]).to eq("translate")
end
end
context "when stability API key is present" do
before { SiteSetting.ai_stability_api_key = "foo" }
it "returns the illustrate_post prompt in the list of all prompts" do
prompts = subject.available_prompts
expect(prompts.length).to eq(7)
expect(prompts.map { |p| p[:name] }).to contain_exactly(
"translate",
"generate_titles",
"proofread",
"markdown_table",
"custom_prompt",
"explain",
"illustrate_post",
)
end
end
end
describe "#generate_and_send_prompt" do describe "#generate_and_send_prompt" do
context "when using a prompt that returns text" do context "when using a prompt that returns text" do
let(:mode) { CompletionPrompt::TRANSLATE } let(:mode) { CompletionPrompt::TRANSLATE }

View File

@ -138,6 +138,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
sign_in(user) sign_in(user)
user.group_ids = [Group::AUTO_GROUPS[:trust_level_1]] user.group_ids = [Group::AUTO_GROUPS[:trust_level_1]]
SiteSetting.ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1] SiteSetting.ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]
SiteSetting.ai_stability_api_key = "foo"
end end
it "returns a list of prompts when no name_filter is provided" do it "returns a list of prompts when no name_filter is provided" do