@@ -63,24 +63,48 @@ function extractBalancedBraces(text: string, startIndex: number): string {
6363}
6464
6565const agentConfigs = parseAgentsFile ( ) ;
66- console . log ( "Loaded agents:" , agentConfigs . length ) ;
6766
6867const featureFiles = [ "page.tsx" , "style.css" , "README.mdx" ]
6968
70- function getFile ( _filePath : string | undefined , _fileName ?: string ) {
69+ async function getFile ( _filePath : string | undefined , _fileName ?: string ) {
7170 if ( ! _filePath ) {
7271 console . warn ( `File path is undefined, skipping.` ) ;
7372 return { }
7473 }
74+
7575 const fileName = _fileName ?? _filePath . split ( '/' ) . pop ( ) ?? ''
7676 const filePath = _fileName ? path . join ( _filePath , fileName ) : _filePath ;
77- if ( ! fs . existsSync ( filePath ) ) {
78- console . warn ( `File not found: ${ filePath } , skipping.` ) ;
79- return { }
80- }
81-
77+
78+ // Check if it's a remote URL
79+ const isRemoteUrl = _filePath . startsWith ( 'http://' ) || _filePath . startsWith ( 'https://' ) ;
80+
81+ let content : string ;
82+
8283 try {
83- const content = fs . readFileSync ( filePath , "utf8" ) ;
84+ if ( isRemoteUrl ) {
85+ // Convert GitHub URLs to raw URLs for direct file access
86+ let fetchUrl = _filePath ;
87+ if ( _filePath . includes ( 'github.com' ) && _filePath . includes ( '/blob/' ) ) {
88+ fetchUrl = _filePath . replace ( 'github.com' , 'raw.githubusercontent.com' ) . replace ( '/blob/' , '/' ) ;
89+ }
90+
91+ // Fetch remote file content
92+ console . log ( `Fetching remote file: ${ fetchUrl } ` ) ;
93+ const response = await fetch ( fetchUrl ) ;
94+ if ( ! response . ok ) {
95+ console . warn ( `Failed to fetch remote file: ${ fetchUrl } , status: ${ response . status } ` ) ;
96+ return { }
97+ }
98+ content = await response . text ( ) ;
99+ } else {
100+ // Handle local file
101+ if ( ! fs . existsSync ( filePath ) ) {
102+ console . warn ( `File not found: ${ filePath } , skipping.` ) ;
103+ return { }
104+ }
105+ content = fs . readFileSync ( filePath , "utf8" ) ;
106+ }
107+
84108 const extension = fileName . split ( "." ) . pop ( ) ;
85109 let language = extension ;
86110 if ( extension === "py" ) language = "python" ;
@@ -95,21 +119,21 @@ function getFile(_filePath: string | undefined, _fileName?: string) {
95119 return {
96120 name : fileName ,
97121 content,
98- // path: path.join(demoIdWithFramework, fileName), // Store relative path within agent/demo
99122 language,
100123 type : 'file'
101124 }
102125 } catch ( error ) {
103126 console . error ( `Error reading file ${ filePath } :` , error ) ;
127+ return { }
104128 }
105129}
106130
107- function getFeatureFrontendFiles ( featureId : string ) {
131+ async function getFeatureFrontendFiles ( featureId : string ) {
108132 const featurePath = path . join ( __dirname , `../src/app/[integrationId]/feature/${ featureId as string } ` ) ;
109133 const retrievedFiles = [ ]
110134
111135 for ( const fileName of featureFiles ) {
112- retrievedFiles . push ( getFile ( featurePath , fileName ) )
136+ retrievedFiles . push ( await getFile ( featurePath , fileName ) )
113137 }
114138
115139 return retrievedFiles ;
@@ -171,23 +195,23 @@ const agentFilesMapper: Record<string, (agentKeys: string[]) => Record<string, s
171195 }
172196}
173197
174- function runGenerateContent ( ) {
198+ async function runGenerateContent ( ) {
175199 const result = { }
176200 for ( const agentConfig of agentConfigs ) {
177201 // Use the parsed agent keys instead of executing the agents function
178202 const agentsPerFeatures = agentConfig . agentKeys
179203
180204 const agentFilePaths = agentFilesMapper [ agentConfig . id ] ( agentConfig . agentKeys )
181205 // Per feature, assign all the frontend files like page.tsx as well as all agent files
182- agentsPerFeatures . forEach ( featureId => {
206+ for ( const featureId of agentsPerFeatures ) {
183207 // @ts -expect-error -- redundant error about indexing of a new object.
184208 result [ `${ agentConfig . id } ::${ featureId } ` ] = [
185209 // Get all frontend files for the feature
186- ...getFeatureFrontendFiles ( featureId ) ,
210+ ...( await getFeatureFrontendFiles ( featureId ) ) ,
187211 // Get the agent (python/TS) file
188- getFile ( agentFilePaths [ featureId ] )
212+ await getFile ( agentFilePaths [ featureId ] )
189213 ]
190- } )
214+ }
191215 }
192216
193217 return result
@@ -239,10 +263,12 @@ function runGenerateContent() {
239263// }
240264// }
241265// }
242- const result = runGenerateContent ( ) ;
243- fs . writeFileSync (
244- path . join ( __dirname , "../src/files.json" ) ,
245- JSON . stringify ( result , null , 2 )
246- ) ;
247-
248- console . log ( "Successfully generated src/files.json" ) ;
266+ ( async ( ) => {
267+ const result = await runGenerateContent ( ) ;
268+ fs . writeFileSync (
269+ path . join ( __dirname , "../src/files.json" ) ,
270+ JSON . stringify ( result , null , 2 )
271+ ) ;
272+
273+ console . log ( "Successfully generated src/files.json" ) ;
274+ } ) ( ) ;
0 commit comments