@@ -1191,6 +1191,7 @@ define(function (require, exports, module) {
11911191 try {
11921192 $target . html ( marked . parse ( _segmentText , { breaks : true , gfm : true } ) ) ;
11931193 _enhanceColorCodes ( $target ) ;
1194+ _addCopyButtons ( $target ) ;
11941195 } catch ( e ) {
11951196 $target . text ( _segmentText ) ;
11961197 }
@@ -1257,6 +1258,35 @@ define(function (require, exports, module) {
12571258 } ) ;
12581259 }
12591260
1261+ /**
1262+ * Inject a copy-to-clipboard button into each <pre> block inside the given container.
1263+ * Idempotent: skips <pre> elements that already have a .ai-copy-btn.
1264+ */
1265+ function _addCopyButtons ( $container ) {
1266+ $container . find ( "pre" ) . each ( function ( ) {
1267+ const $pre = $ ( this ) ;
1268+ if ( $pre . find ( ".ai-copy-btn" ) . length ) {
1269+ return ;
1270+ }
1271+ const $btn = $ ( '<button class="ai-copy-btn" title="' + Strings . AI_CHAT_COPY_CODE + '">' +
1272+ '<i class="fa-solid fa-copy"></i></button>' ) ;
1273+ $btn . on ( "click" , function ( e ) {
1274+ e . stopPropagation ( ) ;
1275+ const $code = $pre . find ( "code" ) ;
1276+ const text = ( $code . length ? $code [ 0 ] : $pre [ 0 ] ) . textContent ;
1277+ Phoenix . app . copyToClipboard ( text ) ;
1278+ const $icon = $btn . find ( "i" ) ;
1279+ $icon . removeClass ( "fa-copy" ) . addClass ( "fa-check" ) ;
1280+ $btn . attr ( "title" , Strings . AI_CHAT_COPIED_CODE ) ;
1281+ setTimeout ( function ( ) {
1282+ $icon . removeClass ( "fa-check" ) . addClass ( "fa-copy" ) ;
1283+ $btn . attr ( "title" , Strings . AI_CHAT_COPY_CODE ) ;
1284+ } , 1500 ) ;
1285+ } ) ;
1286+ $pre . append ( $btn ) ;
1287+ } ) ;
1288+ }
1289+
12601290 function _appendToolIndicator ( toolName , toolId ) {
12611291 // Remove thinking indicator on first content
12621292 if ( ! _hasReceivedContent ) {
@@ -1628,6 +1658,9 @@ define(function (require, exports, module) {
16281658 // Finalize: remove ai-stream-target class so future messages get their own container
16291659 $messages . find ( ".ai-stream-target" ) . removeClass ( "ai-stream-target" ) ;
16301660
1661+ // Ensure copy buttons are present on all code blocks
1662+ _addCopyButtons ( $messages ) ;
1663+
16311664 // Mark all active tool indicators as done
16321665 _finishActiveTools ( ) ;
16331666 }
0 commit comments