11import { hbs } from "ember-cli-htmlbars" ;
2- import { ajax } from "discourse/lib/ajax" ;
3- import { popupAjaxError } from "discourse/lib/ajax-error" ;
42import { withPluginApi } from "discourse/lib/plugin-api" ;
53import { 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 ;
4+ import { withSilencedDeprecations } from "discourse-common/lib/deprecated" ;
115import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon" ;
12- import { showShareConversationModal } from "../discourse/lib/ai-bot-helper" ;
6+ import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button" ;
7+ import AiDebugButton from "../discourse/components/post-menu/ai-debug-button" ;
8+ import AiShareButton from "../discourse/components/post-menu/ai-share-button" ;
9+ import {
10+ isPostFromAiBot ,
11+ showShareConversationModal ,
12+ } from "../discourse/lib/ai-bot-helper" ;
13+ import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers" ;
1314
1415let enabledChatBotIds = [ ] ;
1516let allowDebug = false ;
17+
1618function isGPTBot ( user ) {
1719 return user && enabledChatBotIds . includes ( user . id ) ;
1820}
@@ -22,29 +24,7 @@ function attachHeaderIcon(api) {
2224}
2325
2426function 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- } ) ;
27+ initializePauseButton ( api ) ;
4828
4929 api . modifyClass ( "controller:topic" , {
5030 pluginId : "discourse-ai" ,
@@ -102,34 +82,82 @@ function initializePersonaDecorator(api) {
10282 ) ;
10383}
10484
105- const MAX_PERSONA_USER_ID = - 1200 ;
85+ function initializePauseButton ( api ) {
86+ const transformerRegistered = api . registerValueTransformer (
87+ "post-menu-buttons" ,
88+ ( { value : dag , context : { post, firstButtonKey } } ) => {
89+ if ( isGPTBot ( post . user ) ) {
90+ dag . add ( "ai-cancel-gpt" , AiCancelStreamingButton , {
91+ before : firstButtonKey ,
92+ after : [ "ai-share" , "ai-debug" ] ,
93+ } ) ;
94+ }
95+ }
96+ ) ;
97+
98+ const silencedKey =
99+ transformerRegistered && "discourse.post-menu-widget-overrides" ;
100+
101+ withSilencedDeprecations ( silencedKey , ( ) => initializePauseWidgetButton ( api ) ) ;
102+ }
103+
104+ function initializePauseWidgetButton ( api ) {
105+ api . addPostMenuButton ( "cancel-gpt" , ( post ) => {
106+ if ( isGPTBot ( post . user ) ) {
107+ return {
108+ icon : "pause" ,
109+ action : "cancelStreaming" ,
110+ title : "discourse_ai.ai_bot.cancel_streaming" ,
111+ className : "btn btn-default cancel-streaming" ,
112+ position : "first" ,
113+ } ;
114+ }
115+ } ) ;
116+
117+ api . attachWidgetAction ( "post" , "cancelStreaming" , function ( ) {
118+ AiCancelStreamingButton . cancelStreaming ( this . model ) ;
119+ } ) ;
120+ }
106121
107122function initializeDebugButton ( api ) {
108123 const currentUser = api . getCurrentUser ( ) ;
109124 if ( ! currentUser || ! currentUser . ai_enabled_chat_bots || ! allowDebug ) {
110125 return ;
111126 }
112127
128+ const transformerRegistered = api . registerValueTransformer (
129+ "post-menu-buttons" ,
130+ ( { value : dag , context : { post, firstButtonKey } } ) => {
131+ if ( post . topic ?. archetype === "private_message" ) {
132+ dag . add ( "ai-debug" , AiDebugButton , {
133+ before : firstButtonKey ,
134+ after : "ai-share" ,
135+ } ) ;
136+ }
137+ }
138+ ) ;
139+
140+ const silencedKey =
141+ transformerRegistered && "discourse.post-menu-widget-overrides" ;
142+
143+ withSilencedDeprecations ( silencedKey , ( ) => initializeDebugWidgetButton ( api ) ) ;
144+ }
145+
146+ function initializeDebugWidgetButton ( api ) {
147+ const currentUser = api . getCurrentUser ( ) ;
148+
113149 let debugAiResponse = async function ( { post } ) {
114150 const modal = api . container . lookup ( "service:modal" ) ;
115-
116- modal . show ( DebugAiModal , { model : post } ) ;
151+ AiDebugButton . debugAiResponse ( post , modal ) ;
117152 } ;
118153
119154 api . addPostMenuButton ( "debugAi" , ( post ) => {
120155 if ( post . topic ?. archetype !== "private_message" ) {
121156 return ;
122157 }
123158
124- if (
125- ! currentUser . ai_enabled_chat_bots . any (
126- ( bot ) => post . username === bot . username
127- )
128- ) {
129- // special handling for personas (persona bot users start at ID -1200 and go down)
130- if ( post . user_id > MAX_PERSONA_USER_ID ) {
131- return ;
132- }
159+ if ( ! isPostFromAiBot ( post , currentUser ) ) {
160+ return ;
133161 }
134162
135163 return {
@@ -148,14 +176,29 @@ function initializeShareButton(api) {
148176 return ;
149177 }
150178
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 } ) ;
179+ const transformerRegistered = api . registerValueTransformer (
180+ "post-menu-buttons" ,
181+ ( { value : dag , context : { post, firstButtonKey } } ) => {
182+ if ( post . topic ?. archetype === "private_message" ) {
183+ dag . add ( "ai-share" , AiShareButton , {
184+ before : firstButtonKey ,
185+ } ) ;
186+ }
158187 }
188+ ) ;
189+
190+ const silencedKey =
191+ transformerRegistered && "discourse.post-menu-widget-overrides" ;
192+
193+ withSilencedDeprecations ( silencedKey , ( ) => initializeShareWidgetButton ( api ) ) ;
194+ }
195+
196+ function initializeShareWidgetButton ( api ) {
197+ const currentUser = api . getCurrentUser ( ) ;
198+
199+ let shareAiResponse = async function ( { post, showFeedback } ) {
200+ const modal = api . container . lookup ( "service:modal" ) ;
201+ AiShareButton . shareAiResponse ( post , modal , showFeedback ) ;
159202 } ;
160203
161204 api . addPostMenuButton ( "share" , ( post ) => {
@@ -164,21 +207,14 @@ function initializeShareButton(api) {
164207 return ;
165208 }
166209
167- if (
168- ! currentUser . ai_enabled_chat_bots . any (
169- ( bot ) => post . username === bot . username
170- )
171- ) {
172- // special handling for personas (persona bot users start at ID -1200 and go down)
173- if ( post . user_id > MAX_PERSONA_USER_ID ) {
174- return ;
175- }
210+ if ( ! isPostFromAiBot ( post , currentUser ) ) {
211+ return ;
176212 }
177213
178214 return {
179215 action : shareAiResponse ,
180216 icon : "far-copy" ,
181- className : "post-action-menu__share" ,
217+ className : "post-action-menu__share-ai " ,
182218 title : "discourse_ai.ai_bot.share" ,
183219 position : "first" ,
184220 } ;
@@ -218,10 +254,10 @@ export default {
218254 enabledChatBotIds = user . ai_enabled_chat_bots . map ( ( bot ) => bot . id ) ;
219255 allowDebug = user . can_debug_ai_bot_conversations ;
220256 withPluginApi ( "1.6.0" , attachHeaderIcon ) ;
221- withPluginApi ( "1.6 .0" , initializeAIBotReplies ) ;
257+ withPluginApi ( "1.34 .0" , initializeAIBotReplies ) ;
222258 withPluginApi ( "1.6.0" , initializePersonaDecorator ) ;
223- withPluginApi ( "1.22 .0" , ( api ) => initializeDebugButton ( api , container ) ) ;
224- withPluginApi ( "1.22 .0" , ( api ) => initializeShareButton ( api , container ) ) ;
259+ withPluginApi ( "1.34 .0" , ( api ) => initializeDebugButton ( api , container ) ) ;
260+ withPluginApi ( "1.34 .0" , ( api ) => initializeShareButton ( api , container ) ) ;
225261 withPluginApi ( "1.22.0" , ( api ) =>
226262 initializeShareTopicButton ( api , container )
227263 ) ;
0 commit comments