Skip to content

A platform for community discussion. Free, open, simple.

FIX: allow streaming 2 posts concurrently (#36311)

Samc5610a179a34
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js
index a3972270661..1cc085046d1 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-bot-replies.js
@@ -5,7 +5,10 @@ import AiCancelStreamingButton from "../components/post-menu/ai-cancel-streaming
import AiDebugButton from "../components/post-menu/ai-debug-button";
import AiShareButton from "../components/post-menu/ai-share-button";
import { isGPTBot, showShareConversationModal } from "../lib/ai-bot-helper";
-import { streamPostText } from "../lib/ai-streamer/progress-handlers";
+import {
+ cleanupStreamingData,
+ streamPostText,
+} from "../lib/ai-streamer/progress-handlers";
let allowDebug = false;
@@ -30,16 +33,18 @@ function initializeAIBotReplies(api) {
this.model.details.allowed_users &&
this.model.details.allowed_users.filter(isGPTBot).length >= 1
) {
- // we attempt to recover the last message in the bus
- // so we subscribe at -2
+ // -3 is not obvious, but the implementation in message bus is -2 (last + new), -3 (last 2 + new)
this.messageBus.subscribe(
`discourse-ai/ai-bot/topic/${this.model.id}`,
this.onAIBotStreamedReply.bind(this),
- -2
+ -3
);
}
},
unsubscribe: function () {
+ // we may have infected post stream so lets clean it up
+ cleanupStreamingData(this.model.postStream);
+
this.messageBus.unsubscribe("discourse-ai/ai-bot/topic/*");
this._super();
},
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js
index 86942f12843..7493a840777 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js
@@ -129,6 +129,9 @@ export async function applyProgress(status, updater) {
*/
async function handleProgress(postStream) {
const status = postStream.aiStreamingStatus;
+ if (!status) {
+ return false;
+ }
let keepPolling = false;
@@ -187,6 +190,10 @@ export function streamPostText(postStream, data) {
ensureProgress(postStream);
}
+export function cleanupStreamingData(postStream) {
+ postStream.aiStreamingStatus = undefined;
+}
+
/**
* A generalized function to handle streaming of content using any specified updater class.
* It applies progress updates to the content (raw and cooked) based on the given data.
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
index 433ec7be865..fdd4e7be553 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-conversations-sidebar-manager.js
@@ -14,6 +14,7 @@ import AiBotSidebarEmptyState from "../components/ai-bot-sidebar-empty-state";
export const AI_CONVERSATIONS_PANEL = "ai-conversations";
const SCROLL_BUFFER = 100;
const DEBOUNCE = 100;
+const TITLE_CHANNEL = `/discourse-ai/ai-bot/topic-titles`;
export default class AiConversationsSidebarManager extends Service {
@service appEvents;
@@ -71,6 +72,8 @@ export default class AiConversationsSidebarManager extends Service {
this,
this._attachScrollListener
);
+
+ this._watchForTitleUpdates();
}
willDestroy() {
@@ -85,6 +88,8 @@ export default class AiConversationsSidebarManager extends Service {
this,
this._attachScrollListener
);
+
+ this.messageBus.unsubscribe(TITLE_CHANNEL);
}
addEmptyStateClass() {
@@ -207,22 +212,11 @@ export default class AiConversationsSidebarManager extends Service {
_handleNewBotPM(topic) {
this.topics = [topic, ...this.topics];
this._rebuildSections();
- this._watchForTitleUpdate(topic.id);
}
- _watchForTitleUpdate(topicId) {
- if (this._subscribedTopicIds?.has(topicId)) {
- return;
- }
-
- this._subscribedTopicIds = this._subscribedTopicIds || new Set();
- this._subscribedTopicIds.add(topicId);
-
- const channel = `/discourse-ai/ai-bot/topic/${topicId}`;
-
- this.messageBus.subscribe(channel, (payload) => {
- this._applyTitleUpdate(topicId, payload.title);
- this.messageBus.unsubscribe(channel);
+ _watchForTitleUpdates() {
+ this.messageBus.subscribe(TITLE_CHANNEL, (payload) => {
+ this._applyTitleUpdate(payload.topic_id, payload.title);
});
}
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 047db3fe1a0..f12ba66458c 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -294,6 +294,11 @@ module DiscourseAi
{ title: post.topic.title },
user_ids: allowed_users,
)
+ MessageBus.publish(
+ "/discourse-ai/ai-bot/topic-titles",
+ { title: post.topic.title, topic_id: post.topic.id },
+ user_ids: allowed_users,
+ )
end
def reply_to_chat_message(message, channel, context_post_ids)