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

Commit b5d09a4

Browse files
committed
DEV: Add compatibility with the Glimmer Post Stream
1 parent f43137e commit b5d09a4

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Component from "@glimmer/component";
2+
import { inject as controller } from "@ember/controller";
3+
import { isGPTBot } from "../../lib/ai-bot-helper";
4+
5+
export default class AiPersonaFlair extends Component {
6+
static shouldRender(args) {
7+
return isGPTBot(args.post.user);
8+
}
9+
10+
@controller("topic") topicController;
11+
12+
<template>
13+
<span class="persona-flair">
14+
{{this.topicController.model.ai_persona_name}}
15+
</span>
16+
</template>
17+
}

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: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ import { withSilencedDeprecations } from "discourse/lib/deprecated";
33
import { withPluginApi } from "discourse/lib/plugin-api";
44
import { registerWidgetShim } from "discourse/widgets/render-glimmer";
55
import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon";
6+
import AiPersonaFlair from "../discourse/components/post/ai-persona-flair";
67
import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button";
78
import AiDebugButton from "../discourse/components/post-menu/ai-debug-button";
89
import AiShareButton from "../discourse/components/post-menu/ai-share-button";
910
import {
11+
isGPTBot,
1012
isPostFromAiBot,
1113
showShareConversationModal,
1214
} from "../discourse/lib/ai-bot-helper";
1315
import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers";
1416

15-
let enabledChatBotIds = [];
1617
let allowDebug = false;
1718

18-
function isGPTBot(user) {
19-
return user && enabledChatBotIds.includes(user.id);
20-
}
21-
2219
function attachHeaderIcon(api) {
2320
api.headerIcons.add("ai", AiBotHeaderIcon);
2421
}
@@ -57,6 +54,14 @@ function initializeAIBotReplies(api) {
5754
}
5855

5956
function initializePersonaDecorator(api) {
57+
api.renderAfterWrapperOutlet("post-meta-data-poster-name", AiPersonaFlair);
58+
59+
withSilencedDeprecations("discourse.post-stream-widget-overrides", () =>
60+
initializeWidgetPersonaDecorator(api)
61+
);
62+
}
63+
64+
function initializeWidgetPersonaDecorator(api) {
6065
let topicController = null;
6166
api.decorateWidget(`poster-name:after`, (dec) => {
6267
if (!isGPTBot(dec.attrs.user)) {
@@ -98,7 +103,11 @@ function initializePauseButton(api) {
98103
const silencedKey =
99104
transformerRegistered && "discourse.post-menu-widget-overrides";
100105

101-
withSilencedDeprecations(silencedKey, () => initializePauseWidgetButton(api));
106+
withSilencedDeprecations("discourse.post-stream-widget-overrides", () => {
107+
withSilencedDeprecations(silencedKey, () =>
108+
initializePauseWidgetButton(api)
109+
);
110+
});
102111
}
103112

104113
function initializePauseWidgetButton(api) {
@@ -251,16 +260,16 @@ export default {
251260
const user = container.lookup("service:current-user");
252261

253262
if (user?.ai_enabled_chat_bots) {
254-
enabledChatBotIds = user.ai_enabled_chat_bots.map((bot) => bot.id);
255263
allowDebug = user.can_debug_ai_bot_conversations;
256-
withPluginApi("1.6.0", attachHeaderIcon);
257-
withPluginApi("1.34.0", initializeAIBotReplies);
258-
withPluginApi("1.6.0", initializePersonaDecorator);
259-
withPluginApi("1.34.0", (api) => initializeDebugButton(api, container));
260-
withPluginApi("1.34.0", (api) => initializeShareButton(api, container));
261-
withPluginApi("1.22.0", (api) =>
262-
initializeShareTopicButton(api, container)
263-
);
264+
265+
withPluginApi((api) => {
266+
attachHeaderIcon(api);
267+
initializeAIBotReplies(api);
268+
initializePersonaDecorator(api);
269+
initializeDebugButton(api, container);
270+
initializeShareButton(api, container);
271+
initializeShareTopicButton(api, container);
272+
});
264273
}
265274
},
266275
};

0 commit comments

Comments
 (0)