@@ -63,24 +63,48 @@ function extractBalancedBraces(text: string, startIndex: number): string {
63
63
}
64
64
65
65
const agentConfigs = parseAgentsFile ( ) ;
66
- console . log ( "Loaded agents:" , agentConfigs . length ) ;
67
66
68
67
const featureFiles = [ "page.tsx" , "style.css" , "README.mdx" ]
69
68
70
- function getFile ( _filePath : string | undefined , _fileName ?: string ) {
69
+ async function getFile ( _filePath : string | undefined , _fileName ?: string ) {
71
70
if ( ! _filePath ) {
72
71
console . warn ( `File path is undefined, skipping.` ) ;
73
72
return { }
74
73
}
74
+
75
75
const fileName = _fileName ?? _filePath . split ( '/' ) . pop ( ) ?? ''
76
76
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
+
82
83
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
+
84
108
const extension = fileName . split ( "." ) . pop ( ) ;
85
109
let language = extension ;
86
110
if ( extension === "py" ) language = "python" ;
@@ -95,21 +119,21 @@ function getFile(_filePath: string | undefined, _fileName?: string) {
95
119
return {
96
120
name : fileName ,
97
121
content,
98
- // path: path.join(demoIdWithFramework, fileName), // Store relative path within agent/demo
99
122
language,
100
123
type : 'file'
101
124
}
102
125
} catch ( error ) {
103
126
console . error ( `Error reading file ${ filePath } :` , error ) ;
127
+ return { }
104
128
}
105
129
}
106
130
107
- function getFeatureFrontendFiles ( featureId : string ) {
131
+ async function getFeatureFrontendFiles ( featureId : string ) {
108
132
const featurePath = path . join ( __dirname , `../src/app/[integrationId]/feature/${ featureId as string } ` ) ;
109
133
const retrievedFiles = [ ]
110
134
111
135
for ( const fileName of featureFiles ) {
112
- retrievedFiles . push ( getFile ( featurePath , fileName ) )
136
+ retrievedFiles . push ( await getFile ( featurePath , fileName ) )
113
137
}
114
138
115
139
return retrievedFiles ;
@@ -171,23 +195,23 @@ const agentFilesMapper: Record<string, (agentKeys: string[]) => Record<string, s
171
195
}
172
196
}
173
197
174
- function runGenerateContent ( ) {
198
+ async function runGenerateContent ( ) {
175
199
const result = { }
176
200
for ( const agentConfig of agentConfigs ) {
177
201
// Use the parsed agent keys instead of executing the agents function
178
202
const agentsPerFeatures = agentConfig . agentKeys
179
203
180
204
const agentFilePaths = agentFilesMapper [ agentConfig . id ] ( agentConfig . agentKeys )
181
205
// 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 ) {
183
207
// @ts -expect-error -- redundant error about indexing of a new object.
184
208
result [ `${ agentConfig . id } ::${ featureId } ` ] = [
185
209
// Get all frontend files for the feature
186
- ...getFeatureFrontendFiles ( featureId ) ,
210
+ ...( await getFeatureFrontendFiles ( featureId ) ) ,
187
211
// Get the agent (python/TS) file
188
- getFile ( agentFilePaths [ featureId ] )
212
+ await getFile ( agentFilePaths [ featureId ] )
189
213
]
190
- } )
214
+ }
191
215
}
192
216
193
217
return result
@@ -239,10 +263,12 @@ function runGenerateContent() {
239
263
// }
240
264
// }
241
265
// }
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