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

Commit ede65c9

Browse files
authored
DEV: Add compatibility with the Glimmer Post Stream (#1230)
1 parent 09a6841 commit ede65c9

File tree

5 files changed

+276
-237
lines changed

5 files changed

+276
-237
lines changed

.discourse-compatibility

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
< 3.5.0.beta3-dev: 09a68414804a1447f52e5d60691ba59742cda9ec
12
< 3.5.0.beta2-dev: de8624416a15b3d8e7ad350b083cc1420451ccec
23
< 3.5.0.beta1-dev: bdef136080074a993e7c4f5ca562edc31a8ba756
34
< 3.4.0.beta4-dev: a53719ab8eb071459f215227421b3ea4987e5f87
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Component from "@glimmer/component";
2+
import { isGPTBot } from "../../lib/ai-bot-helper";
3+
4+
export default class AiPersonaFlair extends Component {
5+
static shouldRender(args) {
6+
return isGPTBot(args.post.user);
7+
}
8+
9+
<template>
10+
<span class="persona-flair">
11+
{{@outletArgs.post.topic.ai_persona_name}}
12+
</span>
13+
</template>
14+
}

assets/javascripts/discourse/lib/ai-bot-helper.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
import { ajax } from "discourse/lib/ajax";
22
import { popupAjaxError } from "discourse/lib/ajax-error";
3+
import { getOwnerWithFallback } from "discourse/lib/get-owner";
34
import Composer from "discourse/models/composer";
45
import { i18n } from "discourse-i18n";
56
import ShareFullTopicModal from "../components/modal/share-full-topic-modal";
67

78
const MAX_PERSONA_USER_ID = -1200;
89

10+
let enabledChatBotIds;
11+
12+
export function isGPTBot(user) {
13+
if (!user) {
14+
return;
15+
}
16+
17+
if (!enabledChatBotIds) {
18+
const currentUser = getOwnerWithFallback(this).lookup(
19+
"service:current-user"
20+
);
21+
enabledChatBotIds = currentUser.ai_enabled_chat_bots.map((bot) => bot.id);
22+
}
23+
24+
return enabledChatBotIds.includes(user.id);
25+
}
26+
927
export function isPostFromAiBot(post, currentUser) {
1028
return (
1129
post.user_id <= MAX_PERSONA_USER_ID ||

assets/javascripts/initializers/ai-bot-replies.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { hbs } from "ember-cli-htmlbars";
2+
import { withSilencedDeprecations } from "discourse/lib/deprecated";
23
import { withPluginApi } from "discourse/lib/plugin-api";
34
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
45
import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon";
6+
import AiPersonaFlair from "../discourse/components/post/ai-persona-flair";
57
import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button";
68
import AiDebugButton from "../discourse/components/post-menu/ai-debug-button";
79
import AiShareButton from "../discourse/components/post-menu/ai-share-button";
8-
import { showShareConversationModal } from "../discourse/lib/ai-bot-helper";
10+
import {
11+
isGPTBot,
12+
showShareConversationModal,
13+
} from "../discourse/lib/ai-bot-helper";
914
import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers";
1015

11-
let enabledChatBotIds = [];
1216
let allowDebug = false;
1317

14-
function isGPTBot(user) {
15-
return user && enabledChatBotIds.includes(user.id);
16-
}
17-
1818
function attachHeaderIcon(api) {
1919
api.headerIcons.add("ai", AiBotHeaderIcon);
2020
}
@@ -53,28 +53,28 @@ function initializeAIBotReplies(api) {
5353
}
5454

5555
function initializePersonaDecorator(api) {
56-
let topicController = null;
56+
api.renderAfterWrapperOutlet("post-meta-data-poster-name", AiPersonaFlair);
57+
58+
withSilencedDeprecations("discourse.post-stream-widget-overrides", () =>
59+
initializeWidgetPersonaDecorator(api)
60+
);
61+
}
62+
63+
function initializeWidgetPersonaDecorator(api) {
5764
api.decorateWidget(`poster-name:after`, (dec) => {
5865
if (!isGPTBot(dec.attrs.user)) {
5966
return;
6067
}
61-
// this is hacky and will need to change
62-
// trouble is we need to get the model for the topic
63-
// and it is not available in the decorator
64-
// long term this will not be a problem once we remove widgets and
65-
// have a saner structure for our model
66-
topicController =
67-
topicController || api.container.lookup("controller:topic");
6868

6969
return dec.widget.attach("persona-flair", {
70-
topicController,
70+
personaName: dec.model?.topic?.ai_persona_name,
7171
});
7272
});
7373

7474
registerWidgetShim(
7575
"persona-flair",
7676
"span.persona-flair",
77-
hbs`{{@data.topicController.model.ai_persona_name}}`
77+
hbs`{{@data.personaName}}`
7878
);
7979
}
8080

@@ -159,16 +159,16 @@ export default {
159159
const user = container.lookup("service:current-user");
160160

161161
if (user?.ai_enabled_chat_bots) {
162-
enabledChatBotIds = user.ai_enabled_chat_bots.map((bot) => bot.id);
163162
allowDebug = user.can_debug_ai_bot_conversations;
164-
withPluginApi("1.6.0", attachHeaderIcon);
165-
withPluginApi("1.34.0", initializeAIBotReplies);
166-
withPluginApi("1.6.0", initializePersonaDecorator);
167-
withPluginApi("1.34.0", (api) => initializeDebugButton(api, container));
168-
withPluginApi("1.34.0", (api) => initializeShareButton(api, container));
169-
withPluginApi("1.22.0", (api) =>
170-
initializeShareTopicButton(api, container)
171-
);
163+
164+
withPluginApi((api) => {
165+
attachHeaderIcon(api);
166+
initializeAIBotReplies(api);
167+
initializePersonaDecorator(api);
168+
initializeDebugButton(api, container);
169+
initializeShareButton(api, container);
170+
initializeShareTopicButton(api, container);
171+
});
172172
}
173173
},
174174
};

0 commit comments

Comments
 (0)