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 all 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
21 changes: 19 additions & 2 deletions app/controllers/discourse_ai/ai_helper/assistant_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ 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

RATE_LIMITS = {
"default" => {
amount: 6,
interval: 3.minutes,
},
"caption_image" => {
amount: 20,
interval: 1.minute,
},
}.freeze

def suggest
input = get_text_param!
force_default_locale = params[:force_default_locale] || false
Expand Down Expand Up @@ -161,7 +172,13 @@ def get_post_param!
end

def rate_limiter_performed!
RateLimiter.new(current_user, "ai_assistant", 6, 3.minutes).performed!
action_rate_limit = RATE_LIMITS[action_name] || RATE_LIMITS["default"]
RateLimiter.new(
current_user,
"ai_assistant",
action_rate_limit[:amount],
action_rate_limit[:interval],
).performed!
end

def ensure_can_request_suggestions
Expand Down
11 changes: 9 additions & 2 deletions assets/javascripts/initializers/ai-image-caption.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { extractError, popupAjaxError } from "discourse/lib/ajax-error";
import { apiInitializer } from "discourse/lib/api";
import { getUploadMarkdown, isImage } from "discourse/lib/uploads";
import I18n from "discourse-i18n";
Expand Down 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: extractError(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
36 changes: 36 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,23 @@
expect(response.parsed_body["diff"]).to eq(expected_diff)
end
end

context "when performing numerous requests" do
it "rate limits" do
RateLimiter.enable
rate_limit = described_class::RATE_LIMITS["default"]
amount = rate_limit[:amount]

amount.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 +275,25 @@ def request_caption(params)
end
end
end

context "when performing numerous requests" do
it "rate limits" do
RateLimiter.enable

rate_limit = described_class::RATE_LIMITS["caption_image"]
amount = rate_limit[:amount]

amount.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