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

Commit cdf572e

Browse files
committed
DEV: MessageBus last_id from server side
1 parent 62b42c5 commit cdf572e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

assets/javascripts/discourse/components/ai-post-helper-menu.gjs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export default class AiPostHelperMenu extends Component {
4343
@tracked lastSelectedOption = null;
4444
@tracked isSavingFootnote = false;
4545
@tracked supportsAddFootnote = this.args.data.supportsFastEdit;
46+
@tracked channel =
47+
`/discourse-ai/ai-helper/stream_suggestion/${this.args.data.quoteState.postId}`;
4648

4749
@tracked
4850
smoothStreamer = new SmoothStreamer(
@@ -150,27 +152,26 @@ export default class AiPostHelperMenu extends Component {
150152

151153
@bind
152154
subscribe() {
153-
const channel = `/discourse-ai/ai-helper/stream_suggestion/${this.args.data.quoteState.postId}`;
154155
this.messageBus.subscribe(
155-
channel,
156+
this.channel,
156157
(data, id) => {
157-
this._lastMessageIds[channel] = id;
158+
this._lastMessageIds[this.channel] =
159+
this.args.data.post.discourse_ai_helper_stream_suggestion_last_message_bus_id;
158160
this._updateResult(data, id);
159161
},
160-
this._lastMessageIds[channel]
162+
this._lastMessageIds[this.channel]
161163
);
162164
}
163165

164166
@bind
165167
unsubscribe() {
166-
this.messageBus.unsubscribe(
167-
"/discourse-ai/ai-helper/stream_suggestion/*",
168-
this._updateResult
169-
);
168+
this.messageBus.unsubscribe(this.channel, this._updateResult);
170169
}
171170

172171
@bind
173172
async _updateResult(result) {
173+
this._lastMessageIds[this.channel] =
174+
this.args.data.post.discourse_ai_helper_stream_suggestion_last_message_bus_id;
174175
this.streaming = !result.done;
175176
await this.smoothStreamer.updateResult(result, "result");
176177
}

lib/ai_helper/entry_point.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ def inject_into(plugin)
7373
scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map)
7474
end,
7575
) { object.auto_image_caption }
76+
77+
plugin.add_to_serializer(
78+
:post,
79+
:discourse_ai_helper_stream_suggestion_last_message_bus_id,
80+
include_condition: -> { SiteSetting.ai_helper_enabled && scope.authenticated? },
81+
) { MessageBus.last_id("/discourse-ai/ai-helper/stream_suggestion/#{object.id}") }
7682
end
7783
end
7884
end

spec/system/ai_helper/ai_post_helper_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ def select_post_text(selected_post)
107107

108108
DiscourseAi::Completions::Llm.with_prepared_responses([explain_response]) do
109109
post_ai_helper.select_helper_model(mode)
110-
111-
MessageBus.publish(
112-
"/discourse-ai/ai-helper/stream_suggestion/#{post.id}",
113-
{ result: explain_response, done: true },
114-
user_ids: [user.id],
115-
)
116-
117110
wait_for { post_ai_helper.has_suggestion_value? }
118111
expect(post_ai_helper.suggestion_value).to eq(explain_response)
119112
end

0 commit comments

Comments
 (0)