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 9 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 @@ -39,7 +39,7 @@ export default class AiBotHeaderIcon extends Component {
get clickShouldRouteOutOfConversations() {
return (
!this.navigationMenu.isHeaderDropdownMode &&
this.siteSettings.ai_enable_experimental_bot_ux &&
this.siteSettings.ai_bot_enable_dedicated_ux &&
this.sidebarState.currentPanel?.key === AI_CONVERSATIONS_PANEL
);
}
Expand All @@ -50,7 +50,7 @@ export default class AiBotHeaderIcon extends Component {
return this.router.transitionTo(`discovery.${defaultHomepage()}`);
}

if (this.siteSettings.ai_enable_experimental_bot_ux) {
if (this.siteSettings.ai_bot_enable_dedicated_ux) {
return this.router.transitionTo("discourse-ai-bot-conversations");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,117 @@
import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
import { action } from "@ember/object";
import { getOwner } from "@ember/owner";
import { service } from "@ember/service";
import UppyUpload from "discourse/lib/uppy/uppy-upload";
import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import { clipboardHelpers } from "discourse/lib/utilities";

export default class DiscourseAiBotConversations extends Controller {
@service aiBotConversationsHiddenSubmit;
@service currentUser;
@service mediaOptimizationWorker;
@service site;
@service siteSettings;

@tracked uploads = [];
// Don't track this directly - we'll get it from uppyUpload

textarea = null;
uppyUpload = null;
fileInputEl = null;

_handlePaste = (event) => {
if (document.activeElement !== this.textarea) {
return;
}

const { canUpload, canPasteHtml, types } = clipboardHelpers(event, {
siteSettings: this.siteSettings,
canUpload: true,
});

if (!canUpload || canPasteHtml || types.includes("text/plain")) {
return;
}

if (event && event.clipboardData && event.clipboardData.files) {
this.uppyUpload.addFiles([...event.clipboardData.files], {
pasted: true,
});
}
};
init() {
super.init(...arguments);

this.uploads = [];

this.uppyUpload = new UppyUpload(getOwner(this), {
id: "ai-bot-file-uploader",
type: "ai-bot-conversation",
useMultipartUploadsIfAvailable: true,

uppyReady: () => {
if (this.siteSettings.composer_media_optimization_image_enabled) {
this.uppyUpload.uppyWrapper.useUploadPlugin(UppyMediaOptimization, {
optimizeFn: (data, opts) =>
this.mediaOptimizationWorker.optimizeImage(data, opts),
runParallel: !this.site.isMobileDevice,
});
}

this.uppyUpload.uppyWrapper.onPreProcessProgress((file) => {
const inProgressUpload = this.inProgressUploads?.find(
(upl) => upl.id === file.id
);
if (inProgressUpload && !inProgressUpload.processing) {
inProgressUpload.processing = true;
}
});

this.uppyUpload.uppyWrapper.onPreProcessComplete((file) => {
const inProgressUpload = this.inProgressUploads?.find(
(upl) => upl.id === file.id
);
if (inProgressUpload) {
inProgressUpload.processing = false;
}
});

// Setup paste listener for the textarea
this.textarea?.addEventListener("paste", this._handlePaste);
},

uploadDone: (upload) => {
this.uploads.pushObject(upload);
},

// Fix: Don't try to set inProgressUploads directly
onProgressUploadsChanged: () => {
// This is just for UI triggers - we're already tracking inProgressUploads
this.notifyPropertyChange("inProgressUploads");
},
});
}

willDestroy() {
super.willDestroy(...arguments);
this.textarea?.removeEventListener("paste", this._handlePaste);
this.uppyUpload?.teardown();
}

get loading() {
return this.aiBotConversationsHiddenSubmit?.loading;
}

get inProgressUploads() {
return this.uppyUpload?.inProgressUploads || [];
}

get showUploadsContainer() {
return this.uploads?.length > 0 || this.inProgressUploads?.length > 0;
}

@action
setPersonaId(id) {
this.aiBotConversationsHiddenSubmit.personaId = id;
Expand All @@ -36,13 +132,52 @@ export default class DiscourseAiBotConversations extends Controller {
@action
handleKeyDown(event) {
if (event.key === "Enter" && !event.shiftKey) {
this.aiBotConversationsHiddenSubmit.submitToBot();
this.prepareAndSubmitToBot();
}
}

@action
setTextArea(element) {
this.textarea = element;
if (this.textarea) {
this.textarea.addEventListener("paste", this._handlePaste);
}
}

@action
registerFileInput(element) {
if (element) {
this.fileInputEl = element;
if (this.uppyUpload) {
this.uppyUpload.setup(element);
}
}
}

@action
openFileUpload() {
if (this.fileInputEl) {
this.fileInputEl.click();
}
}

@action
removeUpload(upload) {
this.uploads.removeObject(upload);
}

@action
cancelUpload(upload) {
this.uppyUpload.cancelSingleUpload({
fileId: upload.id,
});
}

@action
prepareAndSubmitToBot() {
// Pass uploads to the service before submitting
this.aiBotConversationsHiddenSubmit.uploads = this.uploads;
this.aiBotConversationsHiddenSubmit.submitToBot();
}

_autoExpandTextarea() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Service, { service } from "@ember/service";
import { tracked } from "@ember-compat/tracked-built-ins";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { getUploadMarkdown } from "discourse/lib/uploads";
import { i18n } from "discourse-i18n";

export default class AiBotConversationsHiddenSubmit extends Service {
Expand All @@ -17,6 +18,7 @@ export default class AiBotConversationsHiddenSubmit extends Service {

personaId;
targetUsername;
uploads = [];

inputValue = "";

Expand All @@ -25,7 +27,7 @@ export default class AiBotConversationsHiddenSubmit extends Service {
this.composer.destroyDraft();
this.composer.close();
next(() => {
document.getElementById("custom-homepage-input").focus();
document.getElementById("ai-bot-conversations-input").focus();
});
}

Expand All @@ -41,26 +43,51 @@ export default class AiBotConversationsHiddenSubmit extends Service {
});
}

// Don't submit if there are still uploads in progress
if (document.querySelector(".ai-bot-upload--in-progress")) {
return this.dialog.alert({
message: i18n("discourse_ai.ai_bot.conversations.uploads_in_progress"),
});
}

this.loading = true;
const title = i18n("discourse_ai.ai_bot.default_pm_prefix");

// Prepare the raw content with any uploads appended
let rawContent = this.inputValue;

// Append upload markdown if we have uploads
if (this.uploads && this.uploads.length > 0) {
rawContent += "\n\n";

this.uploads.forEach((upload) => {
const uploadMarkdown = getUploadMarkdown(upload);
rawContent += uploadMarkdown + "\n";
});
}

try {
const response = await ajax("/posts.json", {
method: "POST",
data: {
raw: this.inputValue,
raw: rawContent,
title,
archetype: "private_message",
target_recipients: this.targetUsername,
meta_data: { ai_persona_id: this.personaId },
},
});

// Reset uploads after successful submission
this.uploads = [];
this.inputValue = "";

this.appEvents.trigger("discourse-ai:bot-pm-created", {
id: response.topic_id,
slug: response.topic_slug,
title,
});

this.router.transitionTo(response.post_url);
} catch (e) {
popupAjaxError(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hash } from "@ember/helper";
import { fn, hash } from "@ember/helper";
import { on } from "@ember/modifier";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import RouteTemplate from "ember-route-template";
Expand All @@ -17,15 +17,24 @@ export default RouteTemplate(
/>

<div class="ai-bot-conversations__content-wrapper">
<h1>{{i18n "discourse_ai.ai_bot.conversations.header"}}</h1>
<div class="ai-bot-conversations__title">
{{i18n "discourse_ai.ai_bot.conversations.header"}}
</div>
<PluginOutlet
@name="ai-bot-conversations-above-input"
@outletArgs={{hash
[email protected]
submit=@controller.aiBotConversationsHiddenSubmit.submitToBot
submit=@controller.prepareAndSubmitToBot
}}
/>

<div class="ai-bot-conversations__input-wrapper">
<DButton
@icon="upload"
@action={{@controller.openFileUpload}}
@title="discourse_ai.ai_bot.conversations.upload_files"
class="btn btn-transparent ai-bot-upload-btn"
/>
<textarea
{{didInsert @controller.setTextArea}}
{{on "input" @controller.updateInputValue}}
Expand All @@ -38,13 +47,54 @@ export default RouteTemplate(
rows="1"
/>
<DButton
@action={{@controller.aiBotConversationsHiddenSubmit.submitToBot}}
@action={{@controller.prepareAndSubmitToBot}}
@icon="paper-plane"
@isLoading={{@controller.loading}}
@title="discourse_ai.ai_bot.conversations.header"
class="ai-bot-button btn-primary ai-conversation-submit"
/>
</div>

{{! Hidden file input element }}
<input
type="file"
id="ai-bot-file-uploader"
class="hidden-upload-field"
multiple="multiple"
{{didInsert @controller.registerFileInput}}
/>

{{#if @controller.showUploadsContainer}}
<div class="ai-bot-conversations__uploads-container">
{{#each @controller.uploads as |upload|}}
<div class="ai-bot-upload">
<span class="ai-bot-upload__filename">
{{upload.original_filename}}
</span>
<DButton
@icon="xmark"
@action={{fn @controller.removeUpload upload}}
class="btn-transparent ai-bot-upload__remove"
/>
</div>
{{/each}}

{{#each @controller.inProgressUploads as |upload|}}
<div class="ai-bot-upload ai-bot-upload--in-progress">
<span class="ai-bot-upload__filename">{{upload.fileName}}</span>
<span class="ai-bot-upload__progress">
{{upload.progress}}%
</span>
<DButton
@icon="xmark"
@action={{fn @controller.cancelUpload upload}}
class="btn-flat ai-bot-upload__remove"
/>
</div>
{{/each}}
</div>
{{/if}}

<p class="ai-disclaimer">
{{i18n "discourse_ai.ai_bot.conversations.disclaimer"}}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
initialize() {
withPluginApi((api) => {
const siteSettings = api.container.lookup("service:site-settings");
if (!siteSettings.ai_enable_experimental_bot_ux) {
if (!siteSettings.ai_bot_enable_dedicated_ux) {
return;
}

Expand Down
Loading