@@ -264,132 +264,58 @@ async function transpileAgent(
264264 fullPath : string ,
265265 verbose : boolean ,
266266) : Promise < string | null > {
267- try {
268- let buildFn : typeof import ( 'esbuild' ) [ 'build' ] | null = null
269- const canUseBunBuild =
270- typeof Bun !== 'undefined' && typeof Bun . build === 'function'
271- try {
272- const esbuildModule = await import ( 'esbuild' )
273- buildFn = esbuildModule . build
274- } catch {
275- // esbuild not available (likely running in compiled binary)
276- }
277-
278- const hash = createHash ( 'sha1' ) . update ( fullPath ) . digest ( 'hex' )
279- // Store compiled agents inside the current project so node module resolution
280- // can find dependencies (e.g. lodash, zod/v4) via parent node_modules.
281- const tempDir = path . join ( process . cwd ( ) , '.codebuff' , 'agents' )
282- const compiledPath = path . join ( tempDir , `${ hash } .mjs` )
283-
284- const buildWithBun = async ( ) : Promise < string | null > => {
285- if ( ! canUseBunBuild ) {
286- return null
287- }
288- const result = await Bun . build ( {
289- entrypoints : [ fullPath ] ,
290- outdir : tempDir ,
291- target : 'node' ,
292- format : 'esm' ,
293- sourcemap : 'inline' ,
294- splitting : false ,
295- minify : false ,
296- root : process . cwd ( ) ,
297- packages : 'external' ,
298- external : [
299- ...builtinModules ,
300- ...builtinModules . map ( ( mod ) => `node:${ mod } ` ) ,
301- ] ,
302- throw : false ,
303- } )
304-
305- if ( ! result . success ) {
306- if ( verbose ) {
307- console . error ( `Bun.build failed for agent: ${ fullPath } ` )
308- }
309- return null
310- }
311-
312- const entryOutput =
313- result . outputs . find ( ( output ) => output . kind === 'entry-point' ) ??
314- result . outputs [ 0 ]
315- const jsText = entryOutput ? await entryOutput . text ( ) : null
316- if ( ! jsText ) {
317- if ( verbose ) {
318- console . error ( `Failed to transpile agent (no output): ${ fullPath } ` )
319- }
320- return null
321- }
322-
323- await fs . promises . mkdir ( tempDir , { recursive : true } )
324- await fs . promises . writeFile ( compiledPath , jsText , 'utf8' )
325- return compiledPath
326- }
267+ const canUseBunBuild =
268+ typeof Bun !== 'undefined' && typeof Bun . build === 'function'
327269
328- if ( buildFn ) {
329- try {
330- const result = await buildFn ( {
331- entryPoints : [ fullPath ] ,
332- absWorkingDir : process . cwd ( ) , // Force esbuild to use current cwd for path resolution
333- bundle : true ,
334- format : 'esm' ,
335- platform : 'node' ,
336- target : 'node18' ,
337- write : false ,
338- logLevel : verbose ? 'info' : 'silent' ,
339- sourcemap : 'inline' ,
340- packages : 'external' ,
341- external : [
342- ...builtinModules ,
343- ...builtinModules . map ( ( mod ) => `node:${ mod } ` ) ,
344- ] ,
345- } )
346-
347- const jsOutput = result . outputFiles ?. [ 0 ]
348- if ( ! jsOutput ?. text ) {
349- if ( verbose ) {
350- console . error ( `Failed to transpile agent (no output): ${ fullPath } ` )
351- }
352- return null
353- }
354-
355- await fs . promises . mkdir ( tempDir , { recursive : true } )
356- await fs . promises . writeFile ( compiledPath , jsOutput . text , 'utf8' )
357-
358- return compiledPath
359- } catch ( error ) {
360- const bunResult = await buildWithBun ( )
361- if ( bunResult ) {
362- return bunResult
363- }
364- if ( verbose ) {
365- console . error (
366- `Error transpiling agent ${ fullPath } :` ,
367- error instanceof Error ? error . message : error ,
368- )
369- }
370- return null
371- }
372- }
373-
374- const bunResult = await buildWithBun ( )
375- if ( bunResult ) {
376- return bunResult
270+ if ( ! canUseBunBuild ) {
271+ if ( verbose ) {
272+ console . error ( `Cannot transpile ${ fullPath } : Bun.build not available` )
377273 }
274+ return null
275+ }
378276
277+ const hash = createHash ( 'sha1' ) . update ( fullPath ) . digest ( 'hex' )
278+ // Store compiled agents inside the current project so node module resolution
279+ // can find dependencies (e.g. lodash, zod/v4) via parent node_modules.
280+ const tempDir = path . join ( process . cwd ( ) , '.codebuff' , 'agents' )
281+ const compiledPath = path . join ( tempDir , `${ hash } .mjs` )
282+
283+ const result = await Bun . build ( {
284+ entrypoints : [ fullPath ] ,
285+ outdir : tempDir ,
286+ target : 'node' ,
287+ format : 'esm' ,
288+ sourcemap : 'inline' ,
289+ splitting : false ,
290+ minify : false ,
291+ root : process . cwd ( ) ,
292+ packages : 'external' ,
293+ external : [
294+ ...builtinModules ,
295+ ...builtinModules . map ( ( mod ) => `node:${ mod } ` ) ,
296+ ] ,
297+ throw : false ,
298+ } )
299+
300+ if ( ! result . success ) {
379301 if ( verbose ) {
380- console . error (
381- `Cannot transpile ${ fullPath } : esbuild not available in compiled binary` ,
382- )
302+ console . error ( `Bun.build failed for agent: ${ fullPath } ` )
383303 }
384304 return null
305+ }
385306
386- } catch ( error ) {
307+ const entryOutput =
308+ result . outputs . find ( ( output ) => output . kind === 'entry-point' ) ??
309+ result . outputs [ 0 ]
310+ const jsText = entryOutput ? await entryOutput . text ( ) : null
311+ if ( ! jsText ) {
387312 if ( verbose ) {
388- console . error (
389- `Error transpiling agent ${ fullPath } :` ,
390- error instanceof Error ? error . message : error ,
391- )
313+ console . error ( `Failed to transpile agent (no output): ${ fullPath } ` )
392314 }
393315 return null
394316 }
317+
318+ await fs . promises . mkdir ( tempDir , { recursive : true } )
319+ await fs . promises . writeFile ( compiledPath , jsText , 'utf8' )
320+ return compiledPath
395321}
0 commit comments