@@ -27,7 +27,7 @@ function fixFilenameCircularRef(): Plugin {
2727 if ( chunk . type === "chunk" ) {
2828 // Replace circular __filename references with direct __filename usage
2929 chunk . code = chunk . code . replace (
30- / c o n s t _ _ f i l e n a m e ( \d + ) = u r l \. f i l e U R L T o P a t h \( t y p e o f d o c u m e n t = = = " u n d e f i n e d " \? r e q u i r e \( " u r l " \) \. p a t h T o F i l e U R L \( _ _ f i l e n a m e \1\) \. h r e f : [ ^ ; ] + \) ; / g,
30+ / c o n s t _ _ f i l e n a m e ( \d + ) = [ \w $ ] + \. f i l e U R L T o P a t h \( t y p e o f d o c u m e n t = = = " u n d e f i n e d " \? r e q u i r e \( " u r l " \) \. p a t h T o F i l e U R L \( _ _ f i l e n a m e \1\) \. h r e f : [ ^ ; ] + \) ; / g,
3131 "const __filename$1 = __filename;" ,
3232 ) ;
3333 }
@@ -66,37 +66,84 @@ function copyClaudeExecutable(): Plugin {
6666 return {
6767 name : "copy-claude-executable" ,
6868 writeBundle ( ) {
69- const sdkDir = join (
70- __dirname ,
71- "node_modules/@posthog/agent/dist/claude-cli/" ,
72- ) ;
73-
74- // IMPORTANT: Copy to claude-cli/ subdirectory to isolate the package.json
75- // If we put package.json in .vite/build/, it breaks Vite's CommonJS output
7669 const destDir = join ( __dirname , ".vite/build/claude-cli" ) ;
7770
7871 if ( ! existsSync ( destDir ) ) {
7972 mkdirSync ( destDir , { recursive : true } ) ;
8073 }
8174
82- const files = [ "cli.js" , "package.json" , "yoga.wasm" ] ;
75+ // Define potential sources for the Claude CLI artifacts
76+ // Priority 1: Pre-built agent package (Production / CI)
77+ // Priority 2: Workspace source files (Development)
78+ const candidates = [
79+ // Local node_modules (standard package structure)
80+ {
81+ path : join ( __dirname , "node_modules/@posthog/agent/dist/claude-cli" ) ,
82+ type : "package" ,
83+ } ,
84+ // Root node_modules (hoisted package)
85+ {
86+ path : join (
87+ __dirname ,
88+ "../../node_modules/@posthog/agent/dist/claude-cli" ,
89+ ) ,
90+ type : "package" ,
91+ } ,
92+ // Direct workspace access (monorepo build)
93+ {
94+ path : join ( __dirname , "../../packages/agent/dist/claude-cli" ) ,
95+ type : "package" ,
96+ } ,
97+ ] ;
8398
84- for ( const file of files ) {
85- const src = join ( sdkDir , file ) ;
86- const dest = join ( destDir , file ) ;
87-
88- if ( ! existsSync ( src ) ) {
89- console . warn (
90- `[copy-claude-executable] ${ file } not found. ` +
91- `Run 'pnpm build' in the agent directory first. `,
99+ // Check if any pre-built candidate exists
100+ for ( const candidate of candidates ) {
101+ if (
102+ existsSync ( join ( candidate . path , "cli.js" ) ) &&
103+ existsSync ( join ( candidate . path , "yoga.wasm" ) )
104+ ) {
105+ console . log (
106+ `[copy-claude-executable] Found pre-built artifacts at ${ candidate . path } `,
92107 ) ;
93- continue ;
108+ const files = [ "cli.js" , "package.json" , "yoga.wasm" ] ;
109+ for ( const file of files ) {
110+ copyFileSync ( join ( candidate . path , file ) , join ( destDir , file ) ) ;
111+ }
112+ console . log ( "Copied Claude CLI to claude-cli/ subdirectory" ) ;
113+ return ;
94114 }
115+ }
95116
96- copyFileSync ( src , dest ) ;
117+ // Fallback: Assemble from individual source packages (Development Workspace)
118+ console . log (
119+ "[copy-claude-executable] Pre-built artifacts not found. Attempting to assemble from workspace sources..." ,
120+ ) ;
121+
122+ const rootNodeModules = join ( __dirname , "../../node_modules" ) ;
123+ const sdkDir = join ( rootNodeModules , "@anthropic-ai/claude-agent-sdk" ) ;
124+ const yogaDir = join ( rootNodeModules , "yoga-wasm-web/dist" ) ;
125+
126+ if (
127+ existsSync ( join ( sdkDir , "cli.js" ) ) &&
128+ existsSync ( join ( yogaDir , "yoga.wasm" ) )
129+ ) {
130+ copyFileSync ( join ( sdkDir , "cli.js" ) , join ( destDir , "cli.js" ) ) ;
131+ copyFileSync (
132+ join ( sdkDir , "package.json" ) ,
133+ join ( destDir , "package.json" ) ,
134+ ) ; // Note: This copies the SDK package.json, which might not be ideal but works for type: module
135+ copyFileSync ( join ( yogaDir , "yoga.wasm" ) , join ( destDir , "yoga.wasm" ) ) ;
136+ console . log (
137+ "Assembled Claude CLI from workspace sources in claude-cli/ subdirectory" ,
138+ ) ;
139+ return ;
97140 }
98141
99- console . log ( "Copied Claude CLI to claude-cli/ subdirectory" ) ;
142+ console . warn (
143+ "[copy-claude-executable] FAILED to find Claude CLI artifacts. Agent execution may fail." ,
144+ ) ;
145+ console . warn ( "Checked paths:" , candidates . map ( ( c ) => c . path ) . join ( ", " ) ) ;
146+ console . warn ( "Checked workspace sources:" , sdkDir ) ;
100147 } ,
101148 } ;
102149}
0 commit comments