1515 */
1616
1717import { resolve , join } from 'node:path' ;
18- import { readdirSync , statSync } from 'node:fs' ;
18+ import * as path from 'node:path' ;
19+ import { globSync } from 'glob' ;
1920import { SfCommand , Flags } from '@salesforce/sf-plugins-core' ;
2021import { AuthInfo , Connection , Messages , SfError , SfProject } from '@salesforce/core' ;
2122import React from 'react' ;
@@ -49,12 +50,13 @@ enum AgentSource {
4950 LOCAL = 'local' ,
5051}
5152
52- type AgentValue = {
53- Id : string ;
54- DeveloperName : string ;
55- source : AgentSource . ORG ;
56- } |
57- { DeveloperName : string ; source : AgentSource . LOCAL ; path : string } ;
53+ type AgentValue =
54+ | {
55+ Id : string ;
56+ DeveloperName : string ;
57+ source : AgentSource . ORG ;
58+ }
59+ | { DeveloperName : string ; source : AgentSource . LOCAL ; path : string } ;
5860
5961// https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-get-started.html#prerequisites
6062export const UNSUPPORTED_AGENTS = [ 'Copilot_for_Salesforce' ] ;
@@ -166,10 +168,10 @@ export default class AgentPreview extends SfCommand<AgentPreviewResult> {
166168
167169 const outputDir = await resolveOutputDir ( flags [ 'output-dir' ] , flags [ 'apex-debug' ] ) ;
168170 // Both classes share the same interface for the methods we need
169- const agentPreview = selectedAgent . source === AgentSource . ORG ?
170- new Preview ( jwtConn , selectedAgent . Id ) :
171- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
172- new AgentSimulate ( jwtConn , selectedAgent . path , true ) as unknown as Preview ;
171+ const agentPreview : Preview | AgentSimulate =
172+ selectedAgent . source === AgentSource . ORG
173+ ? new Preview ( jwtConn , selectedAgent . Id )
174+ : new AgentSimulate ( jwtConn , selectedAgent . path , true ) ;
173175
174176 agentPreview . toggleApexDebugMode ( flags [ 'apex-debug' ] ) ;
175177
@@ -207,10 +209,7 @@ export const validateAgent = (agent: AgentData): boolean => {
207209 return true ;
208210} ;
209211
210- export const getAgentChoices = (
211- agents : AgentData [ ] ,
212- project : SfProject
213- ) : Array < Choice < AgentValue > > => {
212+ export const getAgentChoices = ( agents : AgentData [ ] , project : SfProject ) : Array < Choice < AgentValue > > => {
214213 const choices : Array < Choice < AgentValue > > = [ ] ;
215214
216215 // Add org agents
@@ -229,37 +228,25 @@ export const getAgentChoices = (
229228 } ) ;
230229 }
231230
232- // Add local agents from authoring bundles
233- const localAgents = findAuthoringBundle ( project . getPath ( ) , '*' ) ;
234- if ( localAgents ) {
235- const bundlePath = localAgents . replace ( / \/ [ ^ / ] + $ / , '' ) ; // Get parent directory
236- const agentDirs = readdirSync ( bundlePath ) . filter ( ( dir ) =>
237- statSync ( join ( bundlePath , dir ) ) . isDirectory ( )
238- ) ;
239-
240- agentDirs . forEach ( ( agentDir ) => {
241- choices . push ( {
242- name : `${ agentDir } (local)` ,
243- value : {
244- DeveloperName : agentDir ,
245- source : AgentSource . LOCAL ,
246- path : join ( bundlePath , agentDir ) ,
247- } ,
248- } ) ;
231+ // Add local agents from .agent files
232+ const localAgentPaths = globSync ( '**/*.agent' , { cwd : project . getPath ( ) } ) ;
233+ for ( const agentPath of localAgentPaths ) {
234+ const agentName = path . basename ( agentPath , '.agent' ) ;
235+ choices . push ( {
236+ name : `${ agentName } (local)` ,
237+ value : {
238+ DeveloperName : agentName ,
239+ source : AgentSource . LOCAL ,
240+ path : path . join ( project . getPath ( ) , agentPath ) ,
241+ } ,
249242 } ) ;
250243 }
251244
252245 return choices ;
253246} ;
254247
255-
256- export const getClientAppsFromAuth = ( authInfo : AuthInfo ) : string [ ] => {
257- const config = authInfo . getConnectionOptions ( ) ;
258- const clientApps = Object . entries ( config )
259- . filter ( ( [ key ] ) => key . startsWith ( 'oauthClientApp_' ) )
260- . map ( ( [ , value ] ) => value as string ) ;
261- return clientApps ;
262- } ;
248+ export const getClientAppsFromAuth = ( authInfo : AuthInfo ) : string [ ] =>
249+ Object . keys ( authInfo . getFields ( ) . clientApps ?? { } ) ;
263250
264251export const resolveOutputDir = async (
265252 outputDir : string | undefined ,
0 commit comments