Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 6b5ea38

Browse files
DEV: Improve AI bot conversation submit upload handling (#1497)
Try fix a flaky spec in /ai_bot/homepage_spec.rb by using ember data rather than inspecting the DOM directly to see if there are any in-progress uploads. Also add missing translation for in progress uploads warning.
1 parent d54cd1f commit 6b5ea38

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

assets/javascripts/discourse/components/ai-bot-conversations.gjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,11 @@ export default class AiBotConversations extends Component {
260260

261261
@action
262262
async prepareAndSubmitToBot() {
263-
// Pass uploads to the service before submitting
264-
this.aiBotConversationsHiddenSubmit.uploads = this.uploads;
265263
try {
266-
await this.aiBotConversationsHiddenSubmit.submitToBot();
264+
await this.aiBotConversationsHiddenSubmit.submitToBot({
265+
uploads: this.uploads,
266+
inProgressUploadsCount: this.inProgressUploads.length,
267+
});
267268
this.uploads = new TrackedArray();
268269
} catch (error) {
269270
popupAjaxError(error);

assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default class AiBotConversationsHiddenSubmit extends Service {
1818

1919
personaId;
2020
targetUsername;
21-
uploads = [];
2221

2322
inputValue = "";
2423

@@ -32,7 +31,7 @@ export default class AiBotConversationsHiddenSubmit extends Service {
3231
}
3332

3433
@action
35-
async submitToBot() {
34+
async submitToBot(uploadData) {
3635
if (
3736
this.inputValue.length <
3837
this.siteSettings.min_personal_message_post_length
@@ -48,7 +47,7 @@ export default class AiBotConversationsHiddenSubmit extends Service {
4847
}
4948

5049
// Don't submit if there are still uploads in progress
51-
if (document.querySelector(".ai-bot-upload--in-progress")) {
50+
if (uploadData.inProgressUploadsCount > 0) {
5251
return this.dialog.alert({
5352
message: i18n("discourse_ai.ai_bot.conversations.uploads_in_progress"),
5453
});
@@ -61,10 +60,10 @@ export default class AiBotConversationsHiddenSubmit extends Service {
6160
let rawContent = this.inputValue;
6261

6362
// Append upload markdown if we have uploads
64-
if (this.uploads && this.uploads.length > 0) {
63+
if (uploadData.uploads && uploadData.uploads.length > 0) {
6564
rawContent += "\n\n";
6665

67-
this.uploads.forEach((upload) => {
66+
uploadData.uploads.forEach((upload) => {
6867
const uploadMarkdown = getUploadMarkdown(upload);
6968
rawContent += uploadMarkdown + "\n";
7069
});
@@ -83,7 +82,6 @@ export default class AiBotConversationsHiddenSubmit extends Service {
8382
});
8483

8584
// Reset uploads after successful submission
86-
this.uploads = [];
8785
this.inputValue = "";
8886

8987
this.appEvents.trigger("discourse-ai:bot-pm-created", {

config/locales/client.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,7 @@ en:
872872
last_7_days: "Last 7 days"
873873
last_30_days: "Last 30 days"
874874
upload_files: "Upload files"
875+
uploads_in_progress: "Cannot submit while uploads are in progress"
875876
sentiments:
876877
dashboard:
877878
title: "Sentiment"

spec/system/ai_bot/homepage_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
RSpec.describe "AI Bot - Homepage", type: :system do
4+
let(:cdp) { PageObjects::CDP.new }
45
let(:topic_page) { PageObjects::Pages::Topic.new }
56
let(:composer) { PageObjects::Components::Composer.new }
67
let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new }
@@ -164,6 +165,28 @@
164165
expect(page).to have_no_css(".ai-bot-upload")
165166
end
166167

168+
it "shows an error when trying to submit while uploads are in progress" do
169+
ai_pm_homepage.visit
170+
expect(ai_pm_homepage).to have_homepage
171+
172+
file_path_1 = file_from_fixtures("logo.png", "images").path
173+
file_path_2 = file_from_fixtures("logo.jpg", "images").path
174+
175+
ai_pm_homepage.input.fill_in(with: "Some message to send to AI with uploads")
176+
177+
cdp.with_slow_upload do
178+
attach_file([file_path_1, file_path_2]) do
179+
find(".ai-bot-upload-btn", visible: true).click
180+
end
181+
expect(page).to have_css(".ai-bot-upload--in-progress", count: 2)
182+
183+
ai_pm_homepage.submit
184+
expect(page).to have_content(
185+
I18n.t("js.discourse_ai.ai_bot.conversations.uploads_in_progress"),
186+
)
187+
end
188+
end
189+
167190
it "allows removing an upload before submission" do
168191
ai_pm_homepage.visit
169192
expect(ai_pm_homepage).to have_homepage

0 commit comments

Comments
 (0)