diff --git a/backend/src/build-system/handlers/frontend-code-generate/FileOperationManager.ts b/backend/src/build-system/handlers/frontend-code-generate/FileOperationManager.ts index 0bbe2ea..2dd2141 100644 --- a/backend/src/build-system/handlers/frontend-code-generate/FileOperationManager.ts +++ b/backend/src/build-system/handlers/frontend-code-generate/FileOperationManager.ts @@ -20,11 +20,13 @@ export class FileOperationManager { } private operationCount = 0; - async executeOperations(operations: FileOperation[]): Promise { + async executeOperations(operations: FileOperation[]): Promise { // if (operations.length > 5) { // throw new Error('Maximum 5 operations per fix'); // } + let newFilePath: string | null = null; + for (const op of operations) { try { switch (op.action) { @@ -33,6 +35,7 @@ export class FileOperationManager { break; case 'rename': await this.handleRename(op); + newFilePath = op.renamePath || null; break; } } catch (error) { @@ -42,6 +45,8 @@ export class FileOperationManager { throw error; } } + + return newFilePath; } private async handleWrite(op: FileOperation): Promise { diff --git a/backend/src/build-system/handlers/frontend-code-generate/FixResponseParser.ts b/backend/src/build-system/handlers/frontend-code-generate/FixResponseParser.ts index c14e862..0aca875 100644 --- a/backend/src/build-system/handlers/frontend-code-generate/FixResponseParser.ts +++ b/backend/src/build-system/handlers/frontend-code-generate/FixResponseParser.ts @@ -39,7 +39,7 @@ export class FixResponseParser { }) .filter(Boolean); - this.logger.log('Extracted operations:', operations); + // this.logger.log('Extracted operations:', operations); return operations; } } diff --git a/backend/src/build-system/handlers/frontend-code-generate/FrontendQueueProcessor.ts b/backend/src/build-system/handlers/frontend-code-generate/FrontendQueueProcessor.ts index 501b411..9d5c4f5 100644 --- a/backend/src/build-system/handlers/frontend-code-generate/FrontendQueueProcessor.ts +++ b/backend/src/build-system/handlers/frontend-code-generate/FrontendQueueProcessor.ts @@ -21,8 +21,8 @@ export class FrontendQueueProcessor { constructor( private validator: FrontendCodeValidator, // Path to your frontend project - private queue: CodeTaskQueue, - private context: BuilderContext, // The queue of files to process + private queue: CodeTaskQueue, // The queue of files to process + private context: BuilderContext, private frontendPath: string, ) {} @@ -53,7 +53,7 @@ export class FrontendQueueProcessor { private async processSingleTask(task: FileTask): Promise { this.logger.log(`Processing file task: ${task.filePath}`); - const currentFullFilePath = normalizePath( + let currentFullFilePath = normalizePath( path.resolve(this.frontendPath, task.filePath), ); @@ -83,12 +83,24 @@ export class FrontendQueueProcessor { // 3. Fix the file try { - await this.fixFileGeneric( + const newFilePath = await this.fixFileGeneric( currentFullFilePath, - task.filePath, + task, validationResult.error ?? '', - task.dependenciesPath, ); + + if (newFilePath !== null) { + this.logger.log( + `File was renamed: ${task.filePath} → ${newFilePath}`, + ); + task.filePath = newFilePath; + currentFullFilePath = normalizePath( + path.resolve(this.frontendPath, newFilePath), + ); + this.logger.log( + `Updated currentFullFilePath: ${currentFullFilePath}`, + ); + } } catch (error) { this.logger.error( 'Fix File Generic failed, get error: ' + error.messages, @@ -113,14 +125,15 @@ export class FrontendQueueProcessor { */ private async fixFileGeneric( currentFullFilePath: string, - filePath: string, + task: FileTask, rawErrorText: string, - dependenciesPath: string, - ) { + ): Promise { try { this.logger.log(`Generic fix attempt for file: ${currentFullFilePath}`); const originalContent = readFileSync(currentFullFilePath, 'utf-8'); + this.logger.debug('raw error: ' + rawErrorText); + const fixPrompt = generateFileOperationPrompt(); const commonIssuePrompt = generateCommonErrorPrompt(); @@ -138,12 +151,12 @@ export class FrontendQueueProcessor { { role: 'system', content: fixPrompt }, { role: 'user', - content: ` Current file path that need to be fix: \n ${filePath}`, + content: ` Current file path that need to be fix: \n ${task.filePath}`, }, { role: 'user', content: ` Error messages: \n ${rawErrorText}` }, { role: 'user', - content: ` dependency file Paths: \n ${dependenciesPath}`, + content: ` dependency file Paths: \n ${task.dependenciesPath}`, }, { role: 'user', @@ -163,14 +176,22 @@ export class FrontendQueueProcessor { ); this.logger.debug('Fix Response: ' + fixResponse); - this.logger.debug('dependency file Paths ' + dependenciesPath); + this.logger.debug('dependency file Paths ' + task.dependenciesPath); const parsed_fixResponse = removeCodeBlockFences(fixResponse); - const operations = parser.parse(parsed_fixResponse, filePath); + const operations = parser.parse(parsed_fixResponse, task.filePath); + + const newFilePath = + await fileOperationManager.executeOperations(operations); - await fileOperationManager.executeOperations(operations); + this.logger.log(`Generic fix applied to file: ${task.filePath}`); + + if (newFilePath) { + this.logger.log(`File was renamed: ${task.filePath} → ${newFilePath}`); + return newFilePath; + } - this.logger.log(`Generic fix applied to file: ${filePath}`); + return null; } catch (error) { this.logger.error('Generic Fix file: ' + error.message); }