Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AssistantController < ::ApplicationController
requires_plugin ::DiscourseAi::PLUGIN_NAME
requires_login
before_action :ensure_can_request_suggestions
before_action :rate_limiter_performed!, except: %i[prompts]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sneaking in this small change that was missed previously. The prompts controller action no longer exists as it was removed here #472.

before_action :rate_limiter_performed!

include SecureUploadEndpointHelpers

Expand Down Expand Up @@ -161,7 +161,11 @@ def get_post_param!
end

def rate_limiter_performed!
RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed!
if action_name == "caption_image"
RateLimiter.new(current_user, "ai_assistant_caption_image", 20, 1.minutes).performed!
else
RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed!
end
end

def ensure_can_request_suggestions
Expand Down
9 changes: 8 additions & 1 deletion assets/javascripts/initializers/ai-image-caption.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ export default apiInitializer("1.25.0", (api) => {
});
return response.caption;
} catch (error) {
popupAjaxError(error);
toasts.error({
class: "ai-image-caption-error-toast",
duration: 3000,
data: {
message: I18n.t("discourse_ai.ai_helper.image_caption.error"),
},
});
}
}

Expand All @@ -129,6 +135,7 @@ export default apiInitializer("1.25.0", (api) => {
return;
}

const toasts = api.container.lookup("service:toasts");
// Automatically caption uploaded images
api.addComposerUploadMarkdownResolver(async (upload) => {
const autoCaptionEnabled = currentUser.get(
Expand Down
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ 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)"
confirm: "Enable"
cancel: "Don't ask again"
error: "There was an error generating a caption for the image."
no_content_error: "Add content first to perform AI actions on it"

reviewables:
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/ai_helper/assistant_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@
expect(response.parsed_body["diff"]).to eq(expected_diff)
end
end

context "when performing numerous requests" do
it "rate limits" do
RateLimiter.enable
6.times do
post "/discourse-ai/ai-helper/suggest", params: { mode: mode, text: text_to_proofread }
expect(response.status).to eq(200)
end
DiscourseAi::Completions::Llm.with_prepared_responses([proofread_text]) do
post "/discourse-ai/ai-helper/suggest", params: { mode: mode, text: text_to_proofread }
expect(response.status).to eq(429)
end
end
end
end
end

Expand Down Expand Up @@ -258,6 +272,21 @@ def request_caption(params)
end
end
end

context "when performing numerous requests" do
it "rate limits" do
RateLimiter.enable
20.times do
request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
expect(r.status).to eq(200)
end
end

request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
expect(r.status).to eq(429)
end
end
end
end
end
end
Loading