11import { hbs } from "ember-cli-htmlbars" ;
2- import { ajax } from "discourse/lib/ajax" ;
3- import { popupAjaxError } from "discourse/lib/ajax-error" ;
2+ import {
3+ POST_MENU_COPY_LINK_BUTTON_KEY ,
4+ POST_MENU_LIKE_BUTTON_KEY ,
5+ POST_MENU_SHARE_BUTTON_KEY ,
6+ POST_MENU_SHOW_MORE_BUTTON_KEY ,
7+ } from "discourse/components/post/menu" ;
48import { withPluginApi } from "discourse/lib/plugin-api" ;
59import { registerWidgetShim } from "discourse/widgets/render-glimmer" ;
6- import DebugAiModal from "../discourse/components/modal/debug-ai-modal" ;
7- import ShareModal from "../discourse/components/modal/share-modal" ;
8- import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers" ;
9- import copyConversation from "../discourse/lib/copy-conversation" ;
10- const AUTO_COPY_THRESHOLD = 4 ;
10+ import { withSilencedDeprecations } from "discourse-common/lib/deprecated" ;
1111import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon" ;
12+ import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button" ;
13+ import AiDebugButton from "../discourse/components/post-menu/ai-debug-button" ;
14+ import AiShareButton from "../discourse/components/post-menu/ai-share-button" ;
1215import { showShareConversationModal } from "../discourse/lib/ai-bot-helper" ;
16+ import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers" ;
1317
1418let enabledChatBotIds = [ ] ;
1519let allowDebug = false ;
20+
1621function isGPTBot ( user ) {
1722 return user && enabledChatBotIds . includes ( user . id ) ;
1823}
@@ -22,29 +27,7 @@ function attachHeaderIcon(api) {
2227}
2328
2429function initializeAIBotReplies ( api ) {
25- api . addPostMenuButton ( "cancel-gpt" , ( post ) => {
26- if ( isGPTBot ( post . user ) ) {
27- return {
28- icon : "pause" ,
29- action : "cancelStreaming" ,
30- title : "discourse_ai.ai_bot.cancel_streaming" ,
31- className : "btn btn-default cancel-streaming" ,
32- position : "first" ,
33- } ;
34- }
35- } ) ;
36-
37- api . attachWidgetAction ( "post" , "cancelStreaming" , function ( ) {
38- ajax ( `/discourse-ai/ai-bot/post/${ this . model . id } /stop-streaming` , {
39- type : "POST" ,
40- } )
41- . then ( ( ) => {
42- document
43- . querySelector ( `#post_${ this . model . post_number } ` )
44- . classList . remove ( "streaming" ) ;
45- } )
46- . catch ( popupAjaxError ) ;
47- } ) ;
30+ initializePauseButton ( api ) ;
4831
4932 api . modifyClass ( "controller:topic" , {
5033 pluginId : "discourse-ai" ,
@@ -104,16 +87,87 @@ function initializePersonaDecorator(api) {
10487
10588const MAX_PERSONA_USER_ID = - 1200 ;
10689
90+ function initializePauseButton ( api ) {
91+ const transformerRegistered = api . registerValueTransformer (
92+ "post-menu-buttons" ,
93+ ( { value : dag , context : { post } } ) => {
94+ if ( isGPTBot ( post . user ) ) {
95+ dag . add ( "ai-cancel-gpt" , AiCancelStreamingButton , {
96+ before : [
97+ POST_MENU_LIKE_BUTTON_KEY ,
98+ POST_MENU_COPY_LINK_BUTTON_KEY ,
99+ POST_MENU_SHARE_BUTTON_KEY ,
100+ POST_MENU_SHOW_MORE_BUTTON_KEY ,
101+ ] ,
102+ after : [ "ai-share" , "ai-debug" ] ,
103+ } ) ;
104+ }
105+
106+ return dag ;
107+ }
108+ ) ;
109+
110+ const silencedKey =
111+ transformerRegistered && "discourse.post-menu-widget-overrides" ;
112+
113+ withSilencedDeprecations ( silencedKey , ( ) => initializePauseWidgetButton ( api ) ) ;
114+ }
115+
116+ function initializePauseWidgetButton ( api ) {
117+ api . addPostMenuButton ( "cancel-gpt" , ( post ) => {
118+ if ( isGPTBot ( post . user ) ) {
119+ return {
120+ icon : "pause" ,
121+ action : "cancelStreaming" ,
122+ title : "discourse_ai.ai_bot.cancel_streaming" ,
123+ className : "btn btn-default cancel-streaming" ,
124+ position : "first" ,
125+ } ;
126+ }
127+ } ) ;
128+
129+ api . attachWidgetAction ( "post" , "cancelStreaming" , function ( ) {
130+ AiCancelStreamingButton . cancelStreaming ( this . model ) ;
131+ } ) ;
132+ }
133+
107134function initializeDebugButton ( api ) {
108135 const currentUser = api . getCurrentUser ( ) ;
109136 if ( ! currentUser || ! currentUser . ai_enabled_chat_bots || ! allowDebug ) {
110137 return ;
111138 }
112139
140+ const transformerRegistered = api . registerValueTransformer (
141+ "post-menu-buttons" ,
142+ ( { value : dag , context : { post } } ) => {
143+ if ( post . topic ?. archetype === "private_message" ) {
144+ dag . add ( "ai-debug" , AiDebugButton , {
145+ before : [
146+ POST_MENU_LIKE_BUTTON_KEY ,
147+ POST_MENU_COPY_LINK_BUTTON_KEY ,
148+ POST_MENU_SHARE_BUTTON_KEY ,
149+ POST_MENU_SHOW_MORE_BUTTON_KEY ,
150+ ] ,
151+ after : "ai-share" ,
152+ } ) ;
153+ }
154+
155+ return dag ;
156+ }
157+ ) ;
158+
159+ const silencedKey =
160+ transformerRegistered && "discourse.post-menu-widget-overrides" ;
161+
162+ withSilencedDeprecations ( silencedKey , ( ) => initializeDebugWidgetButton ( api ) ) ;
163+ }
164+
165+ function initializeDebugWidgetButton ( api ) {
166+ const currentUser = api . getCurrentUser ( ) ;
167+
113168 let debugAiResponse = async function ( { post } ) {
114169 const modal = api . container . lookup ( "service:modal" ) ;
115-
116- modal . show ( DebugAiModal , { model : post } ) ;
170+ AiDebugButton . debugAiResponse ( post , modal ) ;
117171 } ;
118172
119173 api . addPostMenuButton ( "debugAi" , ( post ) => {
@@ -148,14 +202,36 @@ function initializeShareButton(api) {
148202 return ;
149203 }
150204
151- let shareAiResponse = async function ( { post, showFeedback } ) {
152- if ( post . post_number <= AUTO_COPY_THRESHOLD ) {
153- await copyConversation ( post . topic , 1 , post . post_number ) ;
154- showFeedback ( "discourse_ai.ai_bot.conversation_shared" ) ;
155- } else {
156- const modal = api . container . lookup ( "service:modal" ) ;
157- modal . show ( ShareModal , { model : post } ) ;
205+ const transformerRegistered = api . registerValueTransformer (
206+ "post-menu-buttons" ,
207+ ( { value : dag , context : { post } } ) => {
208+ if ( post . topic ?. archetype === "private_message" ) {
209+ dag . add ( "ai-share" , AiShareButton , {
210+ before : [
211+ POST_MENU_LIKE_BUTTON_KEY ,
212+ POST_MENU_COPY_LINK_BUTTON_KEY ,
213+ POST_MENU_SHARE_BUTTON_KEY ,
214+ POST_MENU_SHOW_MORE_BUTTON_KEY ,
215+ ] ,
216+ } ) ;
217+ }
218+
219+ return dag ;
158220 }
221+ ) ;
222+
223+ const silencedKey =
224+ transformerRegistered && "discourse.post-menu-widget-overrides" ;
225+
226+ withSilencedDeprecations ( silencedKey , ( ) => initializeShareWidgetButton ( api ) ) ;
227+ }
228+
229+ function initializeShareWidgetButton ( api ) {
230+ const currentUser = api . getCurrentUser ( ) ;
231+
232+ let shareAiResponse = async function ( { post, showFeedback } ) {
233+ const modal = api . container . lookup ( "service:modal" ) ;
234+ AiShareButton . shareAiResponse ( post , modal , showFeedback ) ;
159235 } ;
160236
161237 api . addPostMenuButton ( "share" , ( post ) => {
@@ -178,7 +254,7 @@ function initializeShareButton(api) {
178254 return {
179255 action : shareAiResponse ,
180256 icon : "far-copy" ,
181- className : "post-action-menu__share" ,
257+ className : "post-action-menu__share-ai " ,
182258 title : "discourse_ai.ai_bot.share" ,
183259 position : "first" ,
184260 } ;
@@ -218,10 +294,10 @@ export default {
218294 enabledChatBotIds = user . ai_enabled_chat_bots . map ( ( bot ) => bot . id ) ;
219295 allowDebug = user . can_debug_ai_bot_conversations ;
220296 withPluginApi ( "1.6.0" , attachHeaderIcon ) ;
221- withPluginApi ( "1.6 .0" , initializeAIBotReplies ) ;
297+ withPluginApi ( "1.34 .0" , initializeAIBotReplies ) ;
222298 withPluginApi ( "1.6.0" , initializePersonaDecorator ) ;
223- withPluginApi ( "1.22 .0" , ( api ) => initializeDebugButton ( api , container ) ) ;
224- withPluginApi ( "1.22 .0" , ( api ) => initializeShareButton ( api , container ) ) ;
299+ withPluginApi ( "1.34 .0" , ( api ) => initializeDebugButton ( api , container ) ) ;
300+ withPluginApi ( "1.34 .0" , ( api ) => initializeShareButton ( api , container ) ) ;
225301 withPluginApi ( "1.22.0" , ( api ) =>
226302 initializeShareTopicButton ( api , container )
227303 ) ;
0 commit comments