|
1 | 1 | import { hbs } from "ember-cli-htmlbars"; |
| 2 | +import { withSilencedDeprecations } from "discourse/lib/deprecated"; |
2 | 3 | import { withPluginApi } from "discourse/lib/plugin-api"; |
3 | 4 | import { registerWidgetShim } from "discourse/widgets/render-glimmer"; |
4 | 5 | import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon"; |
| 6 | +import AiPersonaFlair from "../discourse/components/post/ai-persona-flair"; |
5 | 7 | import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button"; |
6 | 8 | import AiDebugButton from "../discourse/components/post-menu/ai-debug-button"; |
7 | 9 | import AiShareButton from "../discourse/components/post-menu/ai-share-button"; |
8 | | -import { showShareConversationModal } from "../discourse/lib/ai-bot-helper"; |
| 10 | +import { |
| 11 | + getBotType, |
| 12 | + isGPTBot, |
| 13 | + showShareConversationModal, |
| 14 | +} from "../discourse/lib/ai-bot-helper"; |
9 | 15 | import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers"; |
10 | 16 |
|
11 | | -let enabledChatBotIds = []; |
12 | 17 | let allowDebug = false; |
13 | 18 |
|
14 | | -function isGPTBot(user) { |
15 | | - return user && enabledChatBotIds.includes(user.id); |
16 | | -} |
17 | | - |
18 | 19 | function attachHeaderIcon(api) { |
19 | 20 | api.headerIcons.add("ai", AiBotHeaderIcon); |
20 | 21 | } |
@@ -53,28 +54,34 @@ function initializeAIBotReplies(api) { |
53 | 54 | } |
54 | 55 |
|
55 | 56 | function initializePersonaDecorator(api) { |
56 | | - let topicController = null; |
| 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) { |
57 | 65 | api.decorateWidget(`poster-name:after`, (dec) => { |
58 | | - if (!isGPTBot(dec.attrs.user)) { |
59 | | - return; |
| 66 | + const botType = getBotType(dec.attrs.user); |
| 67 | + // we have 2 ways of decorating |
| 68 | + // 1. if a bot is a LLM we decorate with persona name |
| 69 | + // 2. if bot is a persona we decorate with LLM name |
| 70 | + if (botType === "llm") { |
| 71 | + return dec.widget.attach("persona-flair", { |
| 72 | + personaName: dec.model?.topic?.ai_persona_name, |
| 73 | + }); |
| 74 | + } else if (botType === "persona") { |
| 75 | + return dec.widget.attach("persona-flair", { |
| 76 | + personaName: dec.model?.llm_name, |
| 77 | + }); |
60 | 78 | } |
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"); |
68 | | - |
69 | | - return dec.widget.attach("persona-flair", { |
70 | | - topicController, |
71 | | - }); |
72 | 79 | }); |
73 | 80 |
|
74 | 81 | registerWidgetShim( |
75 | 82 | "persona-flair", |
76 | 83 | "span.persona-flair", |
77 | | - hbs`{{@data.topicController.model.ai_persona_name}}` |
| 84 | + hbs`{{@data.personaName}}` |
78 | 85 | ); |
79 | 86 | } |
80 | 87 |
|
@@ -159,16 +166,16 @@ export default { |
159 | 166 | const user = container.lookup("service:current-user"); |
160 | 167 |
|
161 | 168 | if (user?.ai_enabled_chat_bots) { |
162 | | - enabledChatBotIds = user.ai_enabled_chat_bots.map((bot) => bot.id); |
163 | 169 | 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 | | - ); |
| 170 | + |
| 171 | + withPluginApi((api) => { |
| 172 | + attachHeaderIcon(api); |
| 173 | + initializeAIBotReplies(api); |
| 174 | + initializePersonaDecorator(api); |
| 175 | + initializeDebugButton(api, container); |
| 176 | + initializeShareButton(api, container); |
| 177 | + initializeShareTopicButton(api, container); |
| 178 | + }); |
172 | 179 | } |
173 | 180 | }, |
174 | 181 | }; |
0 commit comments