@@ -344,7 +344,12 @@ function readFileFromEncodedLocation(encodedLocation) {
344344 const encodedFilePath = encodedLocation . split ( ":" ) [ 0 ] ;
345345 const filePath = decode ( encodedFilePath ) ;
346346 console . log ( `\x1b[36mℹ Decoded file path: ${ filePath } \x1b[0m` ) ;
347- const targetFile = path . join ( process . cwd ( ) , filePath ) ;
347+
348+ // Check if the path is already absolute, if so use it directly
349+ const targetFile = path . isAbsolute ( filePath )
350+ ? filePath
351+ : path . join ( process . cwd ( ) , filePath ) ;
352+
348353 console . log ( `\x1b[36mℹ Reading file: ${ targetFile } \x1b[0m` ) ;
349354 const fileContent = fs . readFileSync ( targetFile , "utf8" ) ;
350355
@@ -752,43 +757,47 @@ function createApp() {
752757
753758 console . log ( `\x1b[36mℹ Received response from backend\x1b[0m` ) ;
754759
755- // Check if this is the new format with modified_content (full file replacement)
756- if ( backendResponse . modified_content ) {
757- // Handle full file replacement
758- const formattedCode = await applyFullFileReplacement (
759- backendResponse . modified_content ,
760- targetFile
761- ) ;
760+ // Check if this is the new CodingAgentOutput format with path and content
761+ if ( backendResponse . path && backendResponse . content !== undefined ) {
762+ console . log ( `\x1b[36mℹ Processing CodingAgentOutput format for path: ${ backendResponse . path } \x1b[0m` ) ;
763+
764+ // Determine the target file path
765+ const targetFilePath = backendResponse . path . startsWith ( '/' )
766+ ? backendResponse . path
767+ : path . join ( process . cwd ( ) , backendResponse . path ) ;
768+
769+ // Format with Prettier
770+ let formattedCode ;
771+ try {
772+ formattedCode = await prettier . format ( backendResponse . content , {
773+ parser : "typescript" ,
774+ semi : true ,
775+ singleQuote : false ,
776+ } ) ;
777+ } catch ( prettierError ) {
778+ console . error ( "Prettier formatting failed:" , prettierError ) ;
779+ // If formatting fails, use the unformatted code
780+ formattedCode = backendResponse . content ;
781+ }
762782
763- return reply . code ( 200 ) . send ( {
764- success : true ,
765- message :
766- backendResponse . message || `Applied AI changes to ${ filePath } ` ,
767- modified_content : formattedCode ,
768- } ) ;
769- } else if (
770- backendResponse . changes &&
771- Array . isArray ( backendResponse . changes )
772- ) {
773- // Handle incremental changes (fallback)
774- const formattedCode = await applyChangesAndFormat (
775- fileContent ,
776- backendResponse . changes ,
777- targetFile
778- ) ;
783+ // Write to file
784+ fs . writeFileSync ( targetFilePath , formattedCode , "utf8" ) ;
785+
786+ console . log ( `\x1b[32m✓ Updated file ${ targetFilePath } with AI-generated content\x1b[0m` ) ;
779787
780788 return reply . code ( 200 ) . send ( {
781789 success : true ,
782- message : `Applied ${ backendResponse . changes . length } AI-suggested changes to ${ filePath } ` ,
790+ message : `Applied AI changes to ${ backendResponse . path } ` ,
783791 modified_content : formattedCode ,
792+ path : backendResponse . path ,
784793 } ) ;
785794 } else {
786795 console . error (
787796 `\x1b[31m✗ Invalid response format: ${ JSON . stringify (
788797 backendResponse
789798 ) } \x1b[0m`
790799 ) ;
791- throw new Error ( "Invalid response format from backend" ) ;
800+ throw new Error ( "Invalid response format from backend - expected path and content fields " ) ;
792801 }
793802 } catch ( apiError ) {
794803 console . error ( "Error applying AI changes:" , apiError ) ;
0 commit comments