@@ -8,6 +8,7 @@ import { EOL } from 'node:os';
88import { join } from 'node:path' ;
99import { readFileSync } from 'node:fs' ;
1010import { SfCommand , Flags } from '@salesforce/sf-plugins-core' ;
11+ import { MultiStageOutput } from '@oclif/multi-stage-output' ;
1112import { Messages , Lifecycle , SfError } from '@salesforce/core' ;
1213import { Agent , findAuthoringBundle } from '@salesforce/agents' ;
1314import { RetrieveResult , RequestStatus } from '@salesforce/source-deploy-retrieve' ;
@@ -49,45 +50,68 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
4950 messages . getMessage ( 'error.afscriptNotFoundAction' ) ,
5051 ] ) ;
5152 }
52-
53+ // Create multi-stage output
54+ const mso = new MultiStageOutput < { agentName : string } > ( {
55+ stages : [ 'Validate Bundle' , 'Publish Agent' , 'Retrieve Metadata' ] ,
56+ title : 'Publishing Agent' ,
57+ data : { agentName : flags [ 'api-name' ] } ,
58+ jsonEnabled : this . jsonEnabled ( ) ,
59+ postStagesBlock : [
60+ {
61+ label : 'Agent Name' ,
62+ type : 'static-key-value' ,
63+ get : ( data ) => data ?. agentName ,
64+ bold : true ,
65+ color : 'cyan' ,
66+ } ,
67+ ] ,
68+ } ) ;
5369 try {
70+ mso . goto ( 'Validate Bundle' ) ;
5471 const targetOrg = flags [ 'target-org' ] ;
5572 const conn = targetOrg . getConnection ( flags [ 'api-version' ] ) ;
5673
5774 // Set up lifecycle listeners for retrieve events
58- Lifecycle . getInstance ( ) . on ( 'scopedPreRetrieve' , ( ) =>
59- Promise . resolve ( this . log ( 'Starting metadata retrieval...' ) )
60- ) ;
75+ Lifecycle . getInstance ( ) . on ( 'scopedPreRetrieve' , ( ) => {
76+ mso . skipTo ( 'Retrieve Metadata' ) ;
77+ return Promise . resolve ( ) ;
78+ } ) ;
6179
6280 Lifecycle . getInstance ( ) . on ( 'scopedPostRetrieve' , ( result : RetrieveResult ) => {
63- const message =
64- result . response . status === RequestStatus . Succeeded
65- ? 'Successfully retrieved metadata'
66- : `Metadata retrieval failed: ${ ensureArray ( result ?. response ?. messages ) . join ( EOL ) } ` ;
67- return Promise . resolve ( this . log ( message ) ) ;
81+ if ( result . response . status === RequestStatus . Succeeded ) {
82+ mso . stop ( ) ;
83+ } else {
84+ const errorMessage = `Metadata retrieval failed: ${ ensureArray ( result ?. response ?. messages ) . join ( EOL ) } ` ;
85+ mso . error ( ) ;
86+ throw new SfError ( errorMessage ) ;
87+ }
88+ return Promise . resolve ( ) ;
6889 } ) ;
6990
7091 // First compile the AF script to get the Agent JSON
71- this . log ( 'Compiling authoring bundle...' ) ;
7292 const agentJson = await Agent . compileAfScript (
7393 conn ,
7494 readFileSync ( join ( authoringBundleDir , `${ flags [ 'api-name' ] } .afscript` ) , 'utf8' )
7595 ) ;
96+ mso . skipTo ( 'Publish Agent' ) ;
7697
7798 // Then publish the Agent JSON to create the agent
78- this . log ( 'Publishing agent...' ) ;
7999 const result = await Agent . publishAgentJson ( conn , this . project ! , agentJson ) ;
100+ mso . stop ( ) ;
80101
81- this . log ( 'Successfully published agent' ) ;
82102 return {
83103 success : true ,
84104 botDeveloperName : result . botDeveloperName ,
85105 } ;
86106 } catch ( error ) {
87107 // Handle validation errors
88108 const err = error instanceof Error ? error : new Error ( String ( error ) ) ;
109+ const errorMessage = messages . getMessage ( 'error.publishFailed' , [ err . message ] ) ;
110+
111+ // Stop the multi-stage output on error
112+ mso . error ( ) ;
89113
90- this . error ( messages . getMessage ( 'error.publishFailed' , [ err . message ] ) ) ;
114+ this . error ( errorMessage ) ;
91115
92116 return {
93117 success : false ,
0 commit comments